Refactored code cleanup logic, closes #9