Adding topological sort