Fix file closing handling