Refactoring away duplicate code