[repacker] ensure lookup map is updated when lookup memory location changes.
diff --git a/src/graph/graph.hh b/src/graph/graph.hh
index b0b6df7..a73d86f 100644
--- a/src/graph/graph.hh
+++ b/src/graph/graph.hh
@@ -291,12 +291,12 @@
 
     check_success (!queue.in_error ());
     check_success (!sorted_graph.in_error ());
-    if (!check_success (new_id == -1))
-      print_orphaned_nodes ();
 
     remap_all_obj_indices (id_map, &sorted_graph);
-
     hb_swap (vertices_, sorted_graph);
+
+    if (!check_success (new_id == -1))
+      print_orphaned_nodes ();
   }
 
   /*
diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh
index de7cc49..937e058 100644
--- a/src/graph/gsubgpos-graph.hh
+++ b/src/graph/gsubgpos-graph.hh
@@ -142,7 +142,8 @@
       + new_sub_tables.iter() | hb_sink (all_new_subtables);
     }
 
-    add_sub_tables (c, this_index, type, all_new_subtables);
+    if (all_new_subtables)
+      add_sub_tables (c, this_index, type, all_new_subtables);
 
     return true;
   }
@@ -150,7 +151,7 @@
   void add_sub_tables (gsubgpos_graph_context_t& c,
                        unsigned this_index,
                        unsigned type,
-                       hb_vector_t<unsigned> subtable_indices)
+                       hb_vector_t<unsigned>& subtable_indices)
   {
     bool is_ext = is_extension (c.table_tag);
     auto& v = c.graph.vertices_[this_index];
@@ -184,6 +185,10 @@
                        (char*) new_lookup;
       c.graph.vertices_[subtable_id].parents.push (this_index);
     }
+
+    // The head location of the lookup has changed, invalidating the lookups map entry
+    // in the context. Update the map.
+    c.lookups.set (this_index, new_lookup);
   }
 
   unsigned create_extension_subtable (gsubgpos_graph_context_t& c,
diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh
index e803602..36bdf24 100644
--- a/src/hb-repacker.hh
+++ b/src/hb-repacker.hh
@@ -82,7 +82,8 @@
   for (unsigned lookup_index : ext_context.lookups.keys ())
   {
     graph::Lookup* lookup = ext_context.lookups.get(lookup_index);
-    lookup->split_subtables_if_needed (ext_context, lookup_index);
+    if (!lookup->split_subtables_if_needed (ext_context, lookup_index))
+      return false;
   }
 
   return true;