Cleanup, refactoring and comments