Added simple cycle detection.