added infinite loop check