Fixed an infinite loop bug