Reworked while loop