moved definitions of variables away from while loop