Fixed infinite loop problem