Pretty printing

This is a final tidy up of the pretty printing branch for merge. WIP.