fixed infinite loop bug