There are 2 problems:
1) You've set the maximum iterations to 50, but then you use a max iterations of 40 to determine whether to draw black.
2) You're using the x and y coordinates of the equation to draw. You need to keep a separate x and y pixel coordinate at which you are drawing.