Fixed a bug in a while loop