fixed a bug with file closing