Index: /test/pyregfi-smoketest.py
===================================================================
--- /test/pyregfi-smoketest.py	(revision 199)
+++ /test/pyregfi-smoketest.py	(revision 200)
@@ -15,23 +15,16 @@
 
 
-def walk_tree(iter):
-    total_keys = 1
-    total_values = iter_values(iter)
-    print "total_values:", total_values
-    
-    for sub_key in iter:
-        print sub_key.keyname
-        
-        print iter.down()
-        num_keys,num_values = walk_tree(iter)
-        total_keys += num_keys
-        total_values += num_values
-        iter.up()
-
-    return (total_keys, total_values)
-
-
 for f in files:
     rf = pyregfi.RegistryFile(f)
     iter = rf.get_key()
-    print walk_tree(iter)
+
+    num_keys = 0
+    num_values = 0
+    # The iterator now walks the entire registry hive, depth-first
+    for key in iter:
+        #print key.keyname
+        num_keys +=1
+        num_values += iter_values(iter)
+
+    print "keys: %d" % num_keys
+    print "values: %d" % num_values
Index: /trunk/include/regfi.h
===================================================================
--- /trunk/include/regfi.h	(revision 199)
+++ /trunk/include/regfi.h	(revision 200)
@@ -1067,12 +1067,9 @@
 
 /** Sets the internal subkey index to the first subkey referenced by the current
- *  key and returns that key.
+ *  key.
  *
  * @param i the iterator
  *
- * @return A newly allocated key structure for the newly referenced first 
- *         subkey, or NULL on failure.  Failure may be due to a lack of any
- *         subkeys or other errors.  Newly allocated keys must be freed with
- *         @ref regfi_free_record.
+ * @return True if the current key has any subkeys, false otherwise.
  *
  * @ingroup regfiIteratorLayer
@@ -1094,12 +1091,9 @@
 
 
-/** Increments the internal subkey index to the next key in the subkey-list and
- *  returns the subkey for that index.
+/** Increments the internal subkey index to the next key in the subkey-list.
  *
  * @param i the iterator
  *
- * @return A newly allocated key structure for the next subkey or NULL on
- *         failure.  Newly allocated keys must be freed with 
- *         @ref regfi_free_record.
+ * @return True if another subkey should exist, false otherwise.
  *
  * @ingroup regfiIteratorLayer
@@ -1123,12 +1117,9 @@
 
 /** Sets the internal value index to the first value referenced by the current
- *  key and returns that value.
+ *  key.
  *
  * @param i the iterator
  *
- * @return  A newly allocated value structure for the newly referenced first
- *          value, or NULL on failure.  Failure may be due to a lack of any
- *          values or other errors.  Newly allocated keys must be freed with
- *          @ref regfi_free_record.
+ * @return True if the current key has any values, false otherwise.
  *
  * @ingroup regfiIteratorLayer
@@ -1150,12 +1141,9 @@
 
 
-/** Increments the internal value index to the next value in the value-list and
- *  returns the value for that index.
+/** Increments the internal value index to the next value in the value-list.
  *
  * @param i the iterator
  *
- * @return  A newly allocated key structure for the next value or NULL on 
- *          failure.  Newly allocated keys must be freed with 
- *          @ref regfi_free_record.
+ * @return True if another value should exist, false otherwise.
  *
  * @ingroup regfiIteratorLayer
Index: /trunk/lib/regfi.c
===================================================================
--- /trunk/lib/regfi.c	(revision 199)
+++ /trunk/lib/regfi.c	(revision 200)
@@ -67,4 +67,5 @@
 void regfi_init()
 {
+fprintf(stderr, "regfi_init called\n");
   int err;
   if((err = pthread_key_create(&regfi_log_key, regfi_log_free)) != 0)
Index: /trunk/python2/regfi/pyregfi.h
===================================================================
--- /trunk/python2/regfi/pyregfi.h	(revision 199)
+++ /trunk/python2/regfi/pyregfi.h	(revision 200)
@@ -49,5 +49,5 @@
 CLASS(KeyIterator, Object)
      PRIVATE REGFI_ITERATOR *iter;
-     PRIVATE bool first_called;
+     PRIVATE bool root_traversed;
 
      KeyIterator METHOD(KeyIterator, Con, struct RegistryFile_t *file, char **path,
@@ -56,6 +56,6 @@
      struct ValueIterator_t *METHOD(KeyIterator, list_values);
 
-     KeyIterator METHOD(KeyIterator, __iter__);
-     REGFI_NK_REC *METHOD(KeyIterator, iternext);
+     void METHOD(KeyIterator, __iter__);
+     BORROWED REGFI_NK_REC *METHOD(KeyIterator, iternext);
 
      int METHOD(KeyIterator, down);
@@ -71,5 +71,5 @@
 
      void METHOD(ValueIterator, __iter__);
-     RawData METHOD(ValueIterator, iternext);
+     REGFI_VK_REC *METHOD(ValueIterator, iternext);
 END_CLASS
 
Index: /trunk/python2/regfi/regfi.c
===================================================================
--- /trunk/python2/regfi/regfi.c	(revision 199)
+++ /trunk/python2/regfi/regfi.c	(revision 200)
@@ -30,5 +30,5 @@
 
   if(!self->reg) {
-    /*RaiseError(ERuntimeError, "REGFI Error: %s", regfi_log_get_str());*/
+    RaiseError(ERuntimeError, "REGFI Error: %s", regfi_log_get_str());
     /*char* e = regfi_log_get_str();*/
     /*fprintf(stderr, "%p\n", e);*/
@@ -61,6 +61,9 @@
 }
 
-static KeyIterator KeyIterator_Con(KeyIterator self, RegistryFile file, char **path,
-                             REGFI_ENCODING encoding) {
+static KeyIterator KeyIterator_Con(KeyIterator self, 
+				   RegistryFile file, 
+				   char **path,
+				   REGFI_ENCODING encoding) 
+{
   self->iter = regfi_iterator_new(file->reg, encoding);
 
@@ -80,6 +83,5 @@
   }
 
-  fprintf(stderr, "Con called\n");
-  self->first_called = false;
+  self->root_traversed = false;
 
   return self;
@@ -88,29 +90,35 @@
 }
 
-static KeyIterator KeyIterator__iter__(KeyIterator self) {
-  return self;
-}
-
-
-static const REGFI_NK_REC* KeyIterator_next(KeyIterator self) {
-
-  fprintf(stderr, "next called\n");
-
-  if(!self->first_called)
+static void KeyIterator__iter__(KeyIterator self) 
+{
+  return;
+}
+
+
+static const REGFI_NK_REC *KeyIterator_next(KeyIterator self) 
+{
+  if(!self->root_traversed)
+    self->root_traversed = true;
+  else if(!regfi_iterator_down(self->iter))
   {
-    regfi_iterator_first_subkey(self->iter);
-    self->first_called = true;
-  }
-  else
-    regfi_iterator_next_subkey(self->iter);
-    
-  return regfi_iterator_cur_subkey(self->iter);
+    do
+    {
+      if(!regfi_iterator_up(self->iter))
+	return NULL;
+    } while(!regfi_iterator_next_subkey(self->iter));
+
+    /* XXX: This is an error condition.  
+     *      Probably should throw an exception or something. */
+    if(!regfi_iterator_down(self->iter))
+      return NULL;
+  }
+
+  regfi_iterator_first_subkey(self->iter);
+  return regfi_iterator_cur_key(self->iter);
 }
 
 
 static int KeyIterator_down(KeyIterator self) {
-  fprintf(stderr, "down cur_subkey: %d\n", self->iter->cur_subkey);
   int result = regfi_iterator_down(self->iter);
-  fprintf(stderr, "down result: %d\n", result);
   regfi_iterator_first_subkey(self->iter);
   regfi_iterator_first_value(self->iter);
@@ -153,9 +161,9 @@
 }
 
-static ValueIterator ValueIterator__iter__(ValueIterator self) {
-  return self;
-}
-
-static RawData ValueIterator_iternext(ValueIterator self) {
+static void ValueIterator__iter__(ValueIterator self) {
+  return;
+}
+
+static REGFI_VK_REC* ValueIterator_iternext(ValueIterator self) {
   RawData result;
   const REGFI_DATA* data;
@@ -172,4 +180,6 @@
   rec = regfi_iterator_cur_value(self->iter);
 
+  return rec;
+
   if(!rec) return NULL;
 
@@ -177,4 +187,5 @@
    *      Instead, make data fetching a method and parse it then. 
    */
+  /*
   data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec);
   if(!data) {
@@ -198,4 +209,5 @@
     break;
   }
+  */
 
   return result;
