Fixed the implementation of topological sorting.