Refactor and remove code duplication.