Fix topological sort