Not sure what I've done wrong...

But it seems there is a "leak" and the screen starts displaying pixels "leaking" from the top-right corner of the cube. By leaking I mean pixels that should be "dead" are becoming "alive" when they shouldn't.

Maybe you should define MAX?