topological sorting should work properly now