Fixed: file handle could be null and lead to crashes