Improve while loop checks