added writing of to output file, fixed parsing