Fixed infinite while loop