fix endless loop bug