Move variable initialization out of while loop