diff --git a/webform_share.module b/webform_share.module
index 2dec2ec..9270243 100644
--- a/webform_share.module
+++ b/webform_share.module
@@ -131,7 +131,10 @@ function webform_share_components_update_form_submit($form, &$form_state) {
     foreach ($original->webform['components'] as $cid => $component) {
       $existing_components[$component['form_key']] = $cid;
     }
-    $next_id = empty($existing_components) ? 1 : max($existing_components) + 1;
+
+    // Get the max cid for this node's webform so we know where our safe
+    // starting point is.
+    $next_id = webform_share_get_next_max_cid($node->nid);
 
     // Overwrite the entire form if the user is updating everything.
     if (empty($form_state['values']['components_only'])) {
@@ -139,6 +142,8 @@ function webform_share_components_update_form_submit($form, &$form_state) {
       $webform['nid'] = $node->nid;
     }
 
+    $old_to_new_mappings = array();
+
     // Map the imported components to the existing webform components.
     $node->webform['components'] = array();
     foreach ($webform['components'] as $index => $component) {
@@ -149,6 +154,20 @@ function webform_share_components_update_form_submit($form, &$form_state) {
       else {
         $cid = $next_id++;
       }
+
+      $old_to_new_mappings[$component['cid']] = $cid;
+      if (!empty($component['pid'])) {
+        // This mapping assumes that parents always come before children.
+        if (empty($old_to_new_mappings[$component['pid']])) {
+          drupal_set_message(t('Unable to find the correct fieldset for %component.', array(
+            '%component' => $component['name'],
+            )), 'error', TRUE);
+        }
+        else {
+          $component['pid'] = $old_to_new_mappings[$component['pid']];
+        }
+      }
+      // Set the new cid.
       $component['cid'] = $cid;
       $component['nid'] = $node->nid;
       $node->webform['components'][$cid] = $component;
@@ -166,6 +185,23 @@ function webform_share_components_update_form_submit($form, &$form_state) {
   $form_state['redirect'] = 'node/' . $node->nid . '/webform';
 }
 
+/**
+ * Retrieves that last "cid" for a node's webform.
+ *
+ * @param int $nid
+ * @return int
+ */
+function webform_share_get_next_max_cid($nid) {
+  $max_cid = db_select('webform_component', 'w')
+    ->fields('w', array('cid'))
+    ->condition('nid', $nid)
+    ->orderBy('cid', 'DESC')
+    ->range(0, 1)
+    ->execute()
+  ->fetchField();
+  return (empty($max_cid) ? 1 : $max_cid);
+}
+
 
 /**
  * Menu callback to generate the webform dump.
