Close file correctly