close the file correctly