Fix infinite while loop