Optimize the first while loop.

Take the calculation of the screen size out of the loop declaration. This way, the screen size is only calculated once, instead of each time the loop executes. 

Add a few spaces here and there to improve formatting.