Optimized topsort a bit