More pythonic and cycle detection