Code cleanup & refactoring