hopefully correct cycle detection