Review code: Refactoring and optimization