Index: /test/pyregfi-smoketest.py
===================================================================
--- /test/pyregfi-smoketest.py	(revision 254)
+++ /test/pyregfi-smoketest.py	(revision 255)
@@ -106,11 +106,16 @@
     global recurseKey_stat
     global recurseValue_stat
-    recurseKey_stat += (key.mtime.low^key.mtime.high - key.max_bytes_subkeyname) * key.flags
+    recurseKey_stat += (key.mtime - key.max_bytes_subkeyname) * key.flags
     for v in key.values:
         recurseValue_stat += (v.data_off - v.data_size) / (1.0 + v.flags) + v.data_in_offset
-        value = key.values[v.name]
-        if v != value:
-            print("WARNING: iterator value '%s' does not match dictionary value '%s'." 
-                  % (v.name, value.name))
+        try:
+            value = key.values[v.name]
+            if v != value:
+                print("WARNING: iterator value '%s' does not match dictionary value '%s'." 
+                      % (v.name, value.name))
+        except Exception as e:
+                print("WARNING: iterator value name '%s' is not retrievable via value list\n"
+                      % (v.name,))
+        
 
 def recurseTree(cur, operation):
@@ -258,4 +263,5 @@
     print("  Security stat: %d" % stat)
 
+
 tests = {
     "iterTallyNames":iterTallyNames,
@@ -286,5 +292,5 @@
     if st not in tests:
         usage()
-        sys.stderr.write("ERROR: %s not a valid test type" % st)
+        sys.stderr.write("ERROR: %s not a valid test type\n\n" % st)
         sys.exit(1)
 
@@ -296,5 +302,6 @@
 start_time = time.time()
 for hname,fh in files:
-    hive = pyregfi.Hive(fh)
+    #hive = pyregfi.Hive(fh)
+    hive = pyregfi.openHive(hname)
     for tname in selected_tests:
         t = tests[tname]
Index: /trunk/lib/regfi.c
===================================================================
--- /trunk/lib/regfi.c	(revision 254)
+++ /trunk/lib/regfi.c	(revision 255)
@@ -1511,5 +1511,5 @@
       regfi_log_add(REGFI_LOG_WARN, "Could not parse cell at offset"
 		    " 0x%.8X while searching for root key.", cur_offset);
-      return NULL;
+      goto error_locked;
     }
 
@@ -1530,4 +1530,8 @@
   }
 
+  return NULL;
+
+ error_locked:
+  regfi_unlock(file, &file->cb_lock, "regfi_find_root_nk");
   return NULL;
 }
Index: /trunk/python/pyregfi/__init__.py
===================================================================
--- /trunk/python/pyregfi/__init__.py	(revision 254)
+++ /trunk/python/pyregfi/__init__.py	(revision 255)
@@ -258,5 +258,6 @@
     # Memory management for most regfi structures is taken care of here
     def __del__(self):
-        regfi.regfi_free_record(self._hive.file, self._base)
+        if self._base:
+            regfi.regfi_free_record(self._hive.file, self._base)
 
 
@@ -683,5 +684,7 @@
     file = None
     raw_file = None
-    _root = None
+    _fh = None
+    #_root = None
+
 
     ## The root Key of this Hive
@@ -723,4 +726,5 @@
         # The fileno method may not exist, or it may throw an exception
         # when called if the file isn't backed with a descriptor.
+        self._fh = fh
         fn = None
         try:
@@ -766,8 +770,6 @@
     
     def __del__(self):
-        regfi.regfi_free(self.file)
-        if self.raw_file != None:
-            self.raw_file = None
-
+        if self.file:
+            regfi.regfi_free(self.file)
 
     def __iter__(self):
Index: /trunk/python/pyregfi/structures.py
===================================================================
--- /trunk/python/pyregfi/structures.py	(revision 254)
+++ /trunk/python/pyregfi/structures.py	(revision 255)
@@ -97,5 +97,5 @@
 
 
-from winsec import *
+from .winsec import *
 
 REGFI_VK._fields_ = [('offset', c_uint32),
Index: /trunk/python/pyregfi/winsec.py
===================================================================
--- /trunk/python/pyregfi/winsec.py	(revision 254)
+++ /trunk/python/pyregfi/winsec.py	(revision 255)
@@ -11,5 +11,5 @@
 import ctypes.util
 from ctypes import *
-import structures
+from .structures import regfi
 
 is_win32 = hasattr(ctypes, 'windll')
@@ -77,7 +77,6 @@
                         ('dacl', POINTER(WINSEC_ACL)),
                         ]
-
-structures.regfi.winsec_sid2str.argtypes = [POINTER(WINSEC_DOM_SID)]
-structures.regfi.winsec_sid2str.restype = POINTER(c_char)
+regfi.winsec_sid2str.argtypes = [POINTER(WINSEC_DOM_SID)]
+regfi.winsec_sid2str.restype = POINTER(c_char)
 
 
@@ -128,5 +127,5 @@
         self.inherited_object = _guid2uuid(ace.inh_guid)
 
-        c_str = structures.regfi.winsec_sid2str(ace.trustee)
+        c_str = regfi.winsec_sid2str(ace.trustee)
         self.trustee = ctypes.cast(c_str, c_char_p).value.decode('utf-8', 'replace')
         libc.free(c_str)
@@ -153,9 +152,9 @@
 
     def __init__(self, sec_desc):
-        c_str = structures.regfi.winsec_sid2str(sec_desc.owner_sid)
+        c_str = regfi.winsec_sid2str(sec_desc.owner_sid)
         self.owner = ctypes.cast(c_str, c_char_p).value.decode('utf-8', 'replace')
         libc.free(c_str)
         
-        c_str = structures.regfi.winsec_sid2str(sec_desc.grp_sid)
+        c_str = regfi.winsec_sid2str(sec_desc.grp_sid)
         self.group = ctypes.cast(c_str, c_char_p).value.decode('utf-8', 'replace')
         libc.free(c_str)
