Fix a small bug in while loop