diff --git a/EntityDependencyIterator.inc b/EntityDependencyIterator.inc
index ad7bed3..96f0c17 100644
--- a/EntityDependencyIterator.inc
+++ b/EntityDependencyIterator.inc
@@ -126,9 +126,14 @@ class EntityDependencyIterator implements RecursiveIterator {
 
       // Don't add dependencies that already were checked.
       foreach ($this->dependencies as $entity_type => $dependencies) {
-        foreach ($dependencies as $entity_id) {
-          if (isset($this->checked[$entity_type][$entity_id])) {
-            unset($this->dependencies[$entity_type][$entity_id]);
+        foreach ($dependencies as $key => $entity_id) {
+          if (($entity_type == $this->entityType && $this->entityId == $entity_id)
+            || isset($this->checked[$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(
@@ -230,7 +235,14 @@ class EntityDependencyIterator implements RecursiveIterator {
    * Moves the current position to the next element.
    */
   public function next() {
-    return next($this->entities);
+    do {
+      $current = next($this->entities);
+      // If reached the end of array, then exit.
+      if (!$current || is_array($current) || !isset($this->entityType)) {
+        break;
+      }
+      // We need to skip all already traversed elements.
+    } while (isset($this->traversed[$this->entityType][$current]));
   }
 
   /**
diff --git a/entity_dependency.test b/entity_dependency.test
index ca3876a..14af22a 100644
--- a/entity_dependency.test
+++ b/entity_dependency.test
@@ -56,6 +56,11 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
     $user = $this->drupalCreateUser();
     $term1 = $this->createTerm();
     $term2 = $this->createTerm();
+    $term3 = $this->createTerm();
+    $term4 = $this->createTerm();
+    $term1->parent = array($term3->tid);
+    taxonomy_term_save($term1);
+
     $node1 = $this->drupalCreateNode(array(
       'type' => 'article',
       'uid' => $user->uid,
@@ -67,14 +72,14 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
       // We fake that nid 1 is the translation of this node, just to test if
       // the dependency works. We don't want to depend on a node reference.
       'tnid' => $node1->nid,
-      'field_tags' => array(LANGUAGE_NONE => array(array('tid' => $term2->tid))),
+      'field_tags' => array(LANGUAGE_NONE => array(array('tid' => $term1->tid), array('tid' => $term2->tid), array('tid' => $term3->tid), array('tid' => $term4->tid))),
     ));
     // This node only has dependencies that should be detected by $node2
     // already.
     $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
@@ -90,25 +95,33 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
       switch ($i) {
         case 0:
           $entity_id = $entity->tid;
-          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term2->tid);
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term3->tid);
           break;
         case 1:
-          $entity_id = $entity->uid;
-          $test = ($entity_type == 'user' && $entity_id == $user->uid);
+          $entity_id = $entity->tid;
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term1->tid);
           break;
         case 2:
           $entity_id = $entity->tid;
-          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term1->tid);
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term2->tid);
           break;
         case 3:
+          $entity_id = $entity->tid;
+          $test = ($entity_type == 'taxonomy_term' && $entity_id == $term4->tid);
+          break;
+        case 4:
+          $entity_id = $entity->uid;
+          $test = ($entity_type == 'user' && $entity_id == $user->uid);
+          break;
+        case 5:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node1->nid);
           break;
-        case 4:
+        case 6:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node2->nid);
           break;
-        case 5:
+        case 7:
           $entity_id = $entity->nid;
           $test = ($entity_type == 'node' && $entity_id == $node3->nid);
           break;
@@ -117,6 +130,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, 8, 'Correct number of entities was iterated over.');
   }
 }
