Refactor repeating code into separate method