Refactoring duplicated code