refactoring duplicate logic