Fixed a possible infinite while loop.