added infinite loop protection