Fixed the infinite loop bug