[repacker] more bug fixes.
diff --git a/src/graph/graph.hh b/src/graph/graph.hh
index 7dc5413..b0b6df7 100644
--- a/src/graph/graph.hh
+++ b/src/graph/graph.hh
@@ -230,6 +230,17 @@
 
   /*
    * Generates a new topological sorting of graph ordered by the shortest
+   * distance to each node if positions are marked as invalid.
+   */
+  void sort_shortest_distance_if_needed ()
+  {
+    if (!positions_invalid) return;
+    sort_shortest_distance ();
+  }
+
+
+  /*
+   * Generates a new topological sorting of graph ordered by the shortest
    * distance to each node.
    */
   void sort_shortest_distance ()
@@ -541,6 +552,9 @@
                    unsigned new_parent_idx,
                    const O* new_offset)
   {
+    distance_invalid = true;
+    positions_invalid = true;
+
     auto& old_v = vertices_[old_parent_idx];
     auto& new_v = vertices_[new_parent_idx];
 
diff --git a/src/graph/gsubgpos-graph.hh b/src/graph/gsubgpos-graph.hh
index 1fa4247..de7cc49 100644
--- a/src/graph/gsubgpos-graph.hh
+++ b/src/graph/gsubgpos-graph.hh
@@ -159,14 +159,14 @@
                       + subtable_indices.length * OT::Offset16::static_size;
     char* buffer = (char*) hb_calloc (1, new_size);
     c.add_buffer (buffer);
+    memcpy (buffer, v.obj.head, v.table_size());
+
     v.obj.head = buffer;
     v.obj.tail = buffer + new_size;
 
-    memcpy (buffer, v.obj.head, v.table_size());
-
     Lookup* new_lookup = (Lookup*) buffer;
-    new_lookup->subTable.len = subTable.len + subtable_indices.length;
 
+    new_lookup->subTable.len = subTable.len + subtable_indices.length;
     unsigned offset_index = subTable.len;
     for (unsigned subtable_id : subtable_indices)
     {
@@ -181,7 +181,7 @@
       link->width = 2;
       link->objidx = subtable_id;
       link->position = (char*) &new_lookup->subTable[offset_index++] -
-                       (char*) &new_lookup->subTable[0];
+                       (char*) new_lookup;
       c.graph.vertices_[subtable_id].parents.push (this_index);
     }
   }
diff --git a/src/hb-repacker.hh b/src/hb-repacker.hh
index 1ee5985..e803602 100644
--- a/src/hb-repacker.hh
+++ b/src/hb-repacker.hh
@@ -314,11 +314,13 @@
   {
     if (recalculate_extensions)
     {
+      DEBUG_MSG (SUBSET_REPACK, nullptr, "Splitting subtables if needed.");
       if (!_presplit_subtables_if_needed (ext_context)) {
         DEBUG_MSG (SUBSET_REPACK, nullptr, "Subtable splitting failed.");
         return nullptr;
       }
 
+      DEBUG_MSG (SUBSET_REPACK, nullptr, "Promoting lookups to extensions if needed.");
       if (!_promote_extensions_if_needed (ext_context)) {
         DEBUG_MSG (SUBSET_REPACK, nullptr, "Extensions promotion failed.");
         return nullptr;
@@ -328,6 +330,8 @@
     DEBUG_MSG (SUBSET_REPACK, nullptr, "Assigning spaces to 32 bit subgraphs.");
     if (sorted_graph.assign_spaces ())
       sorted_graph.sort_shortest_distance ();
+    else
+      sorted_graph.sort_shortest_distance_if_needed ();
   }
 
   unsigned round = 0;