Cleanup + topological sort