clean up; topological sort