fixed bug with the file not being closed after writing to it