Fixed a bug that leads to an infinite loop.