refactor duplicate code into own method