corrected while loop