Added topological sorting.