Added infinite loop detection.