refactoring duplicated code