Refactoring graph ops to graph.py.   Fixing cycle detection