simplified cycle detection algorithm