Replace duplicate code with a method