refactoring to avoid duplication of code