Factor out formatter-related code
Some more refactoring to support different formatters