Major rewrite of formatter.py