improve while loop