A bit of refactoring to remove duplicate code.