Refactor to remove code duplication.