Fixed possible file handle leak