Added code for writing output to flat file