refactored code so shared formatting concerns are handled in a formatter class