Refactor out duplicate code.