Fixed bug in file writer function