This isn't pretty but it does actually write the text to a file. The next step is strip out the diagnostics