Refactor duplicated code into method.