Code review, refactoring and cleanup