Refactoring to reduce code duplication