diff --git a/cfg_utils.py b/cfg_utils.py
index 8f1081e369cf755478ee4865ccc1d77a1b02ffef..39a4e251d8a898c4fbf213ebc854e2f4ac0a1618 100644
--- a/cfg_utils.py
+++ b/cfg_utils.py
@@ -96,9 +96,12 @@ def apply_rule(tree, leafcache, cache_index, ruleset, rule_number):
 
     location = leafcache[cache_index]
 
-    for depth_index in location:
-        opsite = opsite.subnodes[depth_index]
-        print("TMP index ", depth_index, " gives ",opsite)
+    if (location != []):
+        for depth_index in location:
+            opsite = opsite.subnodes[depth_index]
+            print("TMP index ", depth_index, " gives ",opsite)
+    else:
+        opsite = tree
 
     print(opsite)
     walk_the_tree(opsite)
@@ -144,20 +147,22 @@ def apply_rule(tree, leafcache, cache_index, ruleset, rule_number):
 
 
 
-test_tree = TreeNode(symbols.EXPRESSION, [
-                        TreeNode(symbols.ADDOP,[]),
-                        TreeNode(symbols.MULTOP,[
-                                    TreeNode(symbols.EXPRESSION,[]), TreeNode(symbols.TERM,[]),TreeNode(symbols.FACTOR,[])
-                                   ])
-                        ])
+test_tree = TreeNode(symbols.EXPRESSION, [])
 
 walk_the_tree(test_tree)
 
 
 #def next_stage(derivation_tree_with_cached, language_definition):
 
-leafcache = [[1,1]]
-apply_rule(test_tree,leafcache,0,rules,2)
+leafcache = [[]]
+apply_rule(test_tree,leafcache,0,rules,0)
+print("and the tree looks like")
+walk_the_tree(test_tree)
+
+print("updated leafcache")
+print(leafcache)
+
+apply_rule(test_tree,leafcache,2,rules,2)
 
 print("and the tree looks like")
 walk_the_tree(test_tree)