Changed order of read and write to file.