refactor duplicate code into a method