Refactor and reduce duplication.