Refactoring to minimize code duplication