Refactor duplicated code into its own method.