Fixed the file creation and appending logic