Add comments and fix some subtle problems.
Use tasklist to represent rename actions.
Topological sort the renaming tasks.