Simplify a while loop