Use tsort for topological sorting (massive speedup)