Refactor duplicate code into a method.