moved output formatting process to its own function