fixed infinite loop bugs