fixed while loop bug