Mystery solved. Topological sort does not check for cycles