Refactor a bit and generalize output formatting