Improved loop detection.