Fixed while loop issue