fixed error with file writing