Re-structured to spread the code out

Split the actual cleanup into a few sep methods, which make it
less likely to have a fault, and easier to track it down