Fixed infinite loop bug