Fixed the writing to the output file