Tidy up while loop