Refactored to share code for output.