Bugfix, added some more logging, and made cleanup phase more DRY