close file properly