diff --git a/EntityDependencyIterator.inc b/EntityDependencyIterator.inc
index 9ae6272..0a0a981 100644
--- a/EntityDependencyIterator.inc
+++ b/EntityDependencyIterator.inc
@@ -115,6 +115,16 @@ class EntityDependencyIterator implements RecursiveIterator {
       $entities = entity_load($current['type'], array($current['id']));
       $entity = reset($entities);
 
+      // When $entity does not exist (may be the case with references
+      // to deleted taxonomy terms), skip and remove it from $this->entites.
+      if (!$entity) {
+        watchdog('entity_dependency', "Missing entity %id of type %type", array('%id' => $current['id'], '%type' => $current['type']), WATCHDOG_WARNING);
+        // We can't do anything useful with the no-existent entity,
+        // therfore we just remove it.
+        array_shift($this->entities);
+        return FALSE;
+      }
+
       $this->dependencies = module_invoke_all('entity_dependencies', $entity, $current['type']);
       //$this->belongings = module_invoke_all('entity_belongings', $entity, $this->entityType);
 
diff --git a/entity_dependency.test b/entity_dependency.test
index b36f5d3..f949f70 100644
--- a/entity_dependency.test
+++ b/entity_dependency.test
@@ -227,4 +227,43 @@ class EntityDependencyTestCase extends DrupalWebTestCase {
     }
     $this->assertEqual($i, 8, 'Correct number of entities was iterated over.');
   }
+
+  /**
+   * Test worse scenario with nodes, taxonomy and dangling reference to taxonomy term.
+   */
+  function testDanglingTernReferenceIterator() {
+    // Add the 'field_tags' field.
+    $user = $this->drupalCreateUser();
+    $term1 = $this->createTerm();
+    taxonomy_term_save($term1);
+
+    $node1 = $this->drupalCreateNode(array(
+      'type' => 'article',
+      'uid' => $user->uid,
+      'field_tags' => array(LANGUAGE_NONE => array(array('tid' => $term1->tid))),
+    ));
+
+    // Now delete the term (does not cascade to the reference in field tags).
+    taxonomy_term_delete($term1->tid);
+
+    // Add only the last node to the collection. What should come out of the
+    // iterator should be all it's dependencies, and last the node it self.
+    $this->entities = array(
+      array('id' => $node1->nid, 'type' => 'node'),
+    );
+
+    $i = 0;
+    foreach ($this->getIterator() as $entity) {
+      switch ($i) {
+        case 0:
+          $this->assertCorrectEntityOrder($entity, 'user', 'uid', $user->uid);
+          break;
+        case 1:
+          $this->assertCorrectEntityOrder($entity, 'node', 'nid', $node1->nid);
+          break;
+      }
+      $i++;
+    }
+    $this->assertEqual($i, 2, 'Correct number of entities was iterated over.');
+  }
 }
