Some refactoring to reduce code repetition.