Writing to the file. Some bug fixed.