Added infinite loop detection