Updated while loop for less code