Refactoring to avoid code duplication and optimize performance.