refactor duplicate code into methods