added option to suppress pretty printing, refactored code for writing xml