refactored duplicated code into new method