move duplicate code into a method