incremental writing to output file implemented. Potentially slow for large files, but has good contingency
removed bug with variable scope giving addative data
tidied up output so json is ordered nicely