fixed infinite while loop