added topological sort