Code review and minor refactoring