fix infinite loop bug