Refactor to remove duplication.