Extract duplicated code for refactoring