diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php
index d58662e70283a683c8f97e0cd7b3a29e7adf9367..cd7fa8efc534054a5028ceb16733a88012291de6 100644
--- a/CRM/Dedupe/Merger.php
+++ b/CRM/Dedupe/Merger.php
@@ -1603,98 +1603,9 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
     if (!isset($submitted)) {
       $submitted = [];
     }
+    $customFiles = [];
     foreach ($submitted as $key => $value) {
-      if (substr($key, 0, 7) == 'custom_') {
-        $fid = (int) substr($key, 7);
-        if (empty($cFields[$fid])) {
-          continue;
-        }
-        $htmlType = $cFields[$fid]['attributes']['html_type'];
-        switch ($htmlType) {
-          case 'File':
-            $customFiles[] = $fid;
-            unset($submitted["custom_$fid"]);
-            break;
-
-          case 'Select Country':
-          case 'Select State/Province':
-            $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
-            break;
-
-          case 'Select Date':
-            if ($cFields[$fid]['attributes']['is_view']) {
-              $submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
-            }
-            break;
-
-          case 'CheckBox':
-          case 'Multi-Select':
-          case 'Multi-Select Country':
-          case 'Multi-Select State/Province':
-            // Merge values from both contacts for multivalue fields, CRM-4385
-            // get the existing custom values from db.
-            $customParams = ['entityID' => $mainId, $key => TRUE];
-            $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
-            if (!empty($customfieldValues[$key])) {
-              $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
-              if (is_array($existingValue) && !empty($existingValue)) {
-                $mergeValue = $submittedCustomFields = [];
-                if ($value == 'null') {
-                  // CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
-                  $submitted[$key] = $value;
-                }
-                else {
-                  if ($value) {
-                    $submittedCustomFields = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
-                  }
-
-                  // CRM-19653: overwrite or add the existing custom field value with dupicate contact's
-                  // custom field value stored at $submittedCustomValue.
-                  foreach ($submittedCustomFields as $k => $v) {
-                    if ($v != '' && !in_array($v, $mergeValue)) {
-                      $mergeValue[] = $v;
-                    }
-                  }
-
-                  //keep state and country as array format.
-                  //for checkbox and m-select format w/ VALUE_SEPARATOR
-                  if (in_array($htmlType, [
-                    'CheckBox',
-                    'Multi-Select',
-                  ])) {
-                    $submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
-                        $mergeValue
-                      ) . CRM_Core_DAO::VALUE_SEPARATOR;
-                  }
-                  else {
-                    $submitted[$key] = $mergeValue;
-                  }
-                }
-              }
-            }
-            elseif (in_array($htmlType, [
-              'Multi-Select Country',
-              'Multi-Select State/Province',
-            ])) {
-              //we require submitted values should be in array format
-              if ($value) {
-                $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
-                //hack to remove null values from array.
-                $mergeValue = [];
-                foreach ($mergeValueArray as $k => $v) {
-                  if ($v != '') {
-                    $mergeValue[] = $v;
-                  }
-                }
-                $submitted[$key] = $mergeValue;
-              }
-            }
-            break;
-
-          default:
-            break;
-        }
-      }
+      list($cFields, $customFiles, $submitted) = self::processCustomFields($mainId, $key, $cFields, $customFiles, $submitted, $value);
     }
 
     // **** Do file custom fields related migrations
@@ -2317,6 +2228,113 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
     }
   }
 
+  /**
+   * Honestly - what DOES this do - hopefully some refactoring will reveal it's purpose.
+   *
+   * @param $mainId
+   * @param $key
+   * @param $cFields
+   * @param $customFiles
+   * @param $submitted
+   * @param $value
+   *
+   * @return array
+   */
+  protected static function processCustomFields($mainId, $key, $cFields, $customFiles, $submitted, $value) {
+    if (substr($key, 0, 7) == 'custom_') {
+      $fid = (int) substr($key, 7);
+      if (empty($cFields[$fid])) {
+        return [$cFields, $customFiles, $submitted];
+      }
+      $htmlType = $cFields[$fid]['attributes']['html_type'];
+      switch ($htmlType) {
+        case 'File':
+          $customFiles[] = $fid;
+          unset($submitted["custom_$fid"]);
+          break;
+
+        case 'Select Country':
+        case 'Select State/Province':
+          $submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
+          break;
+
+        case 'Select Date':
+          if ($cFields[$fid]['attributes']['is_view']) {
+            $submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
+          }
+          break;
+
+        case 'CheckBox':
+        case 'Multi-Select':
+        case 'Multi-Select Country':
+        case 'Multi-Select State/Province':
+          // Merge values from both contacts for multivalue fields, CRM-4385
+          // get the existing custom values from db.
+          $customParams = ['entityID' => $mainId, $key => TRUE];
+          $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
+          if (!empty($customfieldValues[$key])) {
+            $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
+            if (is_array($existingValue) && !empty($existingValue)) {
+              $mergeValue = $submittedCustomFields = [];
+              if ($value == 'null') {
+                // CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
+                $submitted[$key] = $value;
+              }
+              else {
+                if ($value) {
+                  $submittedCustomFields = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
+                }
+
+                // CRM-19653: overwrite or add the existing custom field value with dupicate contact's
+                // custom field value stored at $submittedCustomValue.
+                foreach ($submittedCustomFields as $k => $v) {
+                  if ($v != '' && !in_array($v, $mergeValue)) {
+                    $mergeValue[] = $v;
+                  }
+                }
+
+                //keep state and country as array format.
+                //for checkbox and m-select format w/ VALUE_SEPARATOR
+                if (in_array($htmlType, [
+                  'CheckBox',
+                  'Multi-Select',
+                ])) {
+                  $submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
+                      $mergeValue
+                    ) . CRM_Core_DAO::VALUE_SEPARATOR;
+                }
+                else {
+                  $submitted[$key] = $mergeValue;
+                }
+              }
+            }
+          }
+          elseif (in_array($htmlType, [
+            'Multi-Select Country',
+            'Multi-Select State/Province',
+          ])) {
+            //we require submitted values should be in array format
+            if ($value) {
+              $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
+              //hack to remove null values from array.
+              $mergeValue = [];
+              foreach ($mergeValueArray as $k => $v) {
+                if ($v != '') {
+                  $mergeValue[] = $v;
+                }
+              }
+              $submitted[$key] = $mergeValue;
+            }
+          }
+          break;
+
+        default:
+          break;
+      }
+    }
+    return [$cFields, $customFiles, $submitted];
+  }
+
   /**
    * Get metadata for the custom fields for the merge.
    *