Fixed an endless loop bug