write output file and formatting