Fixing some logic issues with writing data files.

Just moving some places where the data array is declared
and how the file is written out.