diff --git a/EntityDependencyIterator.inc b/EntityDependencyIterator.inc
index ad7bed3..9c25142 100644
--- a/EntityDependencyIterator.inc
+++ b/EntityDependencyIterator.inc
@@ -126,9 +126,13 @@ class EntityDependencyIterator implements RecursiveIterator {
 
       // Don't add dependencies that already were checked.
       foreach ($this->dependencies as $entity_type => $dependencies) {
-        foreach ($dependencies as $entity_id) {
+        foreach ($dependencies as $key => $entity_id) {
           if (isset($this->checked[$entity_type][$entity_id])) {
-            unset($this->dependencies[$entity_type][$entity_id]);
+            unset($this->dependencies[$entity_type][$key]);
+            // We need to flush empty dependency trees to avoid creation of empty iterators.
+            if (empty($this->dependencies[$entity_type])) {
+              unset($this->dependencies[$entity_type]);
+            }
           }
           else {
             $this->causes[$entity_type][$entity_id] = array(
diff --git a/entity_dependency.test b/entity_dependency.test
index ca3876a..105a140 100644
--- a/entity_dependency.test
+++ b/entity_dependency.test
@@ -56,6 +56,10 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
     $user = $this->drupalCreateUser();
     $term1 = $this->createTerm();
     $term2 = $this->createTerm();
+    $term3 = $this->createTerm();
+    $term1->parent = array($term3->tid);
+    taxonomy_term_save($term1);
+
     $node1 = $this->drupalCreateNode(array(
       'type' => 'article',
       'uid' => $user->uid,
@@ -74,7 +78,7 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
     $node3 = $this->drupalCreateNode(array(
       'type' => 'article',
       'uid' => $user->uid,
-      'field_tags' => array(LANGUAGE_NONE => array(array('tid' => $term1->tid), array('tid' => $term2->tid))),
+      'field_tags' => array(LANGUAGE_NONE => array(array('tid' => $term1->tid), array('tid' => $term2->tid), array('tid' => $term3->tid))),
     ));
 
     // Add only the last node to the collection. What should come out of the
@@ -98,17 +102,21 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
           break;
         case 2:
           $entity_id = $entity->tid;
-          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term1->tid);
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term3->tid);
           break;
         case 3:
+          $entity_id = $entity->tid;
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term1->tid);
+          break;
+        case 4:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node1->nid);
           break;
-        case 4:
+        case 5:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node2->nid);
           break;
-        case 5:
+        case 6:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node3->nid);
           break;
@@ -117,6 +125,6 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
       $this->assertTrue($test, t('%entity_type %entity_id was iterated over, in correct order.', $placeholders));
       $i++;
     }
-    $this->assertEqual($i, 6, 'Correct number of entities was iterated over.');
+    $this->assertEqual($i, 7, 'Correct number of entities was iterated over.');
   }
 }
