diff --git a/modules/cart/commerce_cart.module b/modules/cart/commerce_cart.module
index 37a0887..d784210 100644
--- a/modules/cart/commerce_cart.module
+++ b/modules/cart/commerce_cart.module
@@ -1260,6 +1260,99 @@ function commerce_cart_forms($form_id, $args) {
 }
 
 /**
+ * Find the matching products. Helper function to commerce_cart_add_to_cart_form.
+ *
+ * @param $products 
+ *  An array of product objects.
+ *  
+ * @param $form_state
+ * @param $attempt
+ *  This function is called twice almost identically. This parameter is a flag 
+ *  which switches off one of the if branches for the first time.  
+ * 
+ * @return
+ *   The $matching_products array.
+ */
+function _commerce_cart_matching_products($products, &$form_state, &$type, &$same_type, $attempt) {
+  $matching_products = array();
+  $default_product = NULL;
+  $attribute_names = array();
+  $unchanged_attributes = array();
+
+  foreach ($products as $product_id => $product) {
+    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
+
+    // Store the first product type.
+    if (empty($type)) {
+      $type = $product->type;
+    }
+
+    // If the current product type is different from the first, we are not
+    // dealing with a set of same typed products.
+    if ($product->type != $type) {
+      $same_type = FALSE;
+    }
+
+    // If the form state contains a set of attribute data, use it to try
+    // and determine the default product.
+    $changed_attribute = NULL;
+
+    if (!empty($form_state['values']['attributes'])) {
+      $match = TRUE;
+
+      // Set an array of checked attributes for later comparison against the
+      // default matching product.
+      if (empty($attribute_names)) {
+        $attribute_names = (array) array_diff_key($form_state['values']['attributes'], array('product_select' => ''));
+        $unchanged_attributes = $form_state['values']['unchanged_attributes'];
+      }
+      foreach ($attribute_names as $key => $value) {
+        // If this is the attribute widget that was changed...
+        if ($value != $unchanged_attributes[$key]) {
+          // Store the field name.
+          $changed_attribute = $key;
+        }
+
+        // If a field name has been stored and we've moved past it to
+        // compare the next attribute field...
+        if (!empty($changed_attribute) && $changed_attribute != $key && $attempt == 2) {
+          // Wipe subsequent values from the form state so the attribute
+          // widgets can use the default values from the new default product.
+          unset($form_state['input']['attributes'][$key]);
+          // Don't accept this as a matching product.
+          continue;
+        }
+
+        if ($product_wrapper->{$key}->raw() != $value) {
+          $match = FALSE;
+        }
+      }
+
+      // If the changed field name has already been stored, only accept the
+      // first matching product by ignoring the rest that would match. An
+      // exception is granted for additional matching products that share
+      // the exact same attribute values as the first.
+      if ($match && !empty($changed_attribute) && !empty($matching_products)) {
+        reset($matching_products);
+        $matching_product = $matching_products[key($matching_products)];
+        $matching_product_wrapper = entity_metadata_wrapper('commerce_product', $matching_product);
+
+        foreach ($attribute_names as $key => $value) {
+          if ($product_wrapper->{$key}->raw() != $matching_product_wrapper->{$key}->raw()) {
+            $match = FALSE;
+          }
+        }
+      }
+
+      if ($match) {
+        $matching_products[$product_id] = $product;
+      }
+    }
+  }
+  return $matching_products;
+}
+
+/**
  * Builds an Add to Cart form for a set of products.
  *
  * @param $line_item
@@ -1381,83 +1474,12 @@ function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_q
       // various Add to Cart form widgets and an array of any matching product
       // based on attribute selections so we can add a selection widget.
       $matching_products = array();
-      $default_product = NULL;
-      $attribute_names = array();
-      $unchanged_attributes = array();
-
-      foreach ($products as $product_id => $product) {
-        $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
-
-        // Store the first product type.
-        if (empty($type)) {
-          $type = $product->type;
-        }
-
-        // If the current product type is different from the first, we are not
-        // dealing with a set of same typed products.
-        if ($product->type != $type) {
-          $same_type = FALSE;
-        }
-
-        // If the form state contains a set of attribute data, use it to try
-        // and determine the default product.
-        $changed_attribute = NULL;
-
-        if (!empty($form_state['values']['attributes'])) {
-          $match = TRUE;
-
-          // Set an array of checked attributes for later comparison against the
-          // default matching product.
-          if (empty($attribute_names)) {
-            $attribute_names = (array) array_diff_key($form_state['values']['attributes'], array('product_select' => ''));
-            $unchanged_attributes = $form_state['values']['unchanged_attributes'];
-          }
-
-          foreach ($attribute_names as $key => $value) {
-            // If this is the attribute widget that was changed...
-            if ($value != $unchanged_attributes[$key]) {
-              // Store the field name.
-              $changed_attribute = $key;
-            }
-
-            // If a field name has been stored and we've moved past it to
-            // compare the next attribute field...
-            if (!empty($changed_attribute) && $changed_attribute != $key) {
-              // Wipe subsequent values from the form state so the attribute
-              // widgets can use the default values from the new default product.
-              unset($form_state['input']['attributes'][$key]);
 
-              // Don't accept this as a matching product.
-              continue;
-            }
-
-            if ($product_wrapper->{$key}->raw() != $value) {
-              $match = FALSE;
-            }
-          }
-
-          // If the changed field name has already been stored, only accept the
-          // first matching product by ignoring the rest that would match. An
-          // exception is granted for additional matching products that share
-          // the exact same attribute values as the first.
-          if ($match && !empty($changed_attribute) && !empty($matching_products)) {
-            reset($matching_products);
-            $matching_product = $matching_products[key($matching_products)];
-            $matching_product_wrapper = entity_metadata_wrapper('commerce_product', $matching_product);
-
-            foreach ($attribute_names as $key => $value) {
-              if ($product_wrapper->{$key}->raw() != $matching_product_wrapper->{$key}->raw()) {
-                $match = FALSE;
-              }
-            }
-          }
-
-          if ($match) {
-            $matching_products[$product_id] = $product;
-          }
-        }
+      $matching_products = _commerce_cart_matching_products($products, $form_state, $type, $same_type, 1);
+      // If no matching products, do it the old way.
+      if (empty($matching_products)) {
+        $matching_products = _commerce_cart_matching_products($products, $form_state, $type, $same_type, 2);
       }
-
       // Set the default product now if it isn't already set.
       if (empty($matching_products)) {
         // If a product ID value was passed in, use that product if it exists.
