diff --git a/conditional_fields.module b/conditional_fields.module
index fb3fcb3..bdef273 100644
--- a/conditional_fields.module
+++ b/conditional_fields.module
@@ -383,43 +383,7 @@ function conditional_fields_attach_dependency(&$form, $dependee, $dependent, $op
   // Actual processing is done in
   // \Drupal\conditional_fields\ConditionalFieldsFormHelper::afterBuild().
   // Append the property so the callback runs last.
-  _conditional_fields_element_add_property($form, '#after_build', [ConditionalFieldsFormHelper::class, 'afterBuild'], 'append');
-}
-
-/**
- * Helper function to add a property/value pair to a render array.
- *
- * Safely without overriding any pre-existing value.
- *
- * @param string $position
- *   Use 'append' if $value should be inserted at the end of
- *   the $element[$property] array, any other value to insert it
- *   at the beginning.
- */
-function _conditional_fields_element_add_property(&$element, $property, $value, $position = 'prepend') {
-  // Avoid overriding default element properties that might not yet be set.
-  if (!isset($element[$property])) {
-    $element[$property] = isset($element['#type']) ? element_info_property($element['#type'], $property, []) : [];
-  }
-  if (is_array($value)) {
-    // A method callback, wrap it around.
-    $value = [$value];
-  }
-
-  if (in_array($value, $element[$property])) {
-    return;
-  }
-
-  switch ($position) {
-    case 'append':
-      $element[$property] = array_merge($element[$property], (array) $value);
-      break;
-
-    case 'prepend':
-    default:
-      $element[$property] = array_merge((array) $value, $element[$property]);
-      break;
-  }
+  ConditionalFieldsFormHelper::elementAddProperty($form, '#after_build', [ConditionalFieldsFormHelper::class, 'afterBuild'], 'append');
 }
 
 /**
diff --git a/src/ConditionalFieldsFormHelper.php b/src/ConditionalFieldsFormHelper.php
index b503efa..9cd65bd 100644
--- a/src/ConditionalFieldsFormHelper.php
+++ b/src/ConditionalFieldsFormHelper.php
@@ -25,17 +25,13 @@ class ConditionalFieldsFormHelper {
     if (empty($form['#conditional_fields'])) {
       return $form;
     }
-
-    $effects = [];
-
     $form_obj = $form_state->getFormObject();
-    if (method_exists($form_obj, 'getFormDisplay')) {
-      /** @var EntityFormDisplayInterface $form_display */
-      $form_display = $form_obj->getFormDisplay($form_state);
-    }
-    else {
+    if (!method_exists($form_obj, 'getFormDisplay')) {
       return $form;
     }
+    // @var EntityFormDisplayInterface $form_display
+    $form_display = $form_obj->getFormDisplay($form_state);
+    $effects = [];
 
     // Cycle all dependents.
     foreach ($form['#conditional_fields'] as $dependent => $dependent_info) {
@@ -83,7 +79,7 @@ class ConditionalFieldsFormHelper {
           'checked',
           '!checked',
         ])) {
-        _conditional_fields_element_add_property($dependent_form_field, '#element_validate', [self::class, 'dependentValidate'], 'append');
+        self::elementAddProperty($dependent_form_field, '#element_validate', [self::class, 'dependentValidate'], 'append');
         }
 
         self::addStateToGroup($states, $state, $options);
@@ -100,37 +96,8 @@ class ConditionalFieldsFormHelper {
       // Save the modified field back into the form.
       NestedArray::setValue($form, $dependent_location, $dependent_form_field);
 
-      // Map the states based on the conjunctions.
-      $states_new = [];
-      foreach ($states as $state_key => $value) {
-        // As the main object is ANDed together we can add the AND items directly.
-        if (!empty($states[$state_key]['AND'])) {
-          $states_new[$state_key] = $states[$state_key]['AND'];
-        }
-        // The OR and XOR groups are moved into a sub-array that has numeric keys
-        // so that we get a JSON array and not an object, as required by the States
-        // API for OR and XOR groupings.
-        if (!empty($states[$state_key]['OR'])) {
-          $or = [];
-          foreach ($states[$state_key]['OR'] as $constraint_key => $constraint_value) {
-            $or[] = [$constraint_key => $constraint_value];
-          }
-          // '1' as a string so that we get an object (which means logic groups
-          // are ANDed together).
-          $states_new[$state_key]['1'] = $or;
-        }
-        if (!empty($states[$state_key]['XOR'])) {
-          $xor = ['xor'];
-          foreach ($states[$state_key]['XOR'] as $constraint_key => $constraint_value) {
-            $xor[] = [$constraint_key => $constraint_value];
-          }
-          // '2' as a string so that we get an object.
-          $states_new[$state_key]['2'] = $xor;
-        }
-      }
-      $states = $states_new;
-
       // Add the #states property to the dependent field.
+      $states = self::mapStates($states);
       NestedArray::setValue($form, array_merge($dependent_location, ['#states']), $states);
     }
 
@@ -663,24 +630,97 @@ class ConditionalFieldsFormHelper {
     }
 
     switch ($options['values_set']) {
-    case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
-      $diff = array_diff($options['values'], $reference_values);
-      return empty($diff);
+      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
+        $diff = array_diff($options['values'], $reference_values);
+        return empty($diff);
 
-    case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
-      $intersect = array_intersect($options['values'], $reference_values);
-      return !empty($intersect);
+      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
+        $intersect = array_intersect($options['values'], $reference_values);
+        return !empty($intersect);
 
-    case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
-      $intersect = array_intersect($options['values'], $reference_values);
-      return count($intersect) == 1;
+      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
+        $intersect = array_intersect($options['values'], $reference_values);
+        return count($intersect) == 1;
 
-    case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
-      $intersect = array_intersect($options['values'], $reference_values);
-      return empty($intersect);
+      case CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
+        $intersect = array_intersect($options['values'], $reference_values);
+        return empty($intersect);
     }
 
     return TRUE;
   }
 
+  /**
+   * Helper function to add a property/value pair to a render array.
+   *
+   * Safely without overriding any pre-existing value.
+   *
+   * @param string $position
+   *   Use 'append' if $value should be inserted at the end of
+   *   the $element[$property] array, any other value to insert it
+   *   at the beginning.
+   */
+  public static function elementAddProperty(&$element, $property, $value, $position = 'prepend') {
+    // Avoid overriding default element properties that might not yet be set.
+    if (!isset($element[$property])) {
+      $element[$property] = isset($element['#type']) ? element_info_property($element['#type'], $property, []) : [];
+    }
+    if (is_array($value)) {
+      // A method callback, wrap it around.
+      $value = [$value];
+    }
+    if (in_array($value, $element[$property])) {
+      return;
+    }
+    switch ($position) {
+      case 'append':
+        $element[$property] = array_merge($element[$property], (array) $value);
+        break;
+      case 'prepend':
+      default:
+        $element[$property] = array_merge((array) $value, $element[$property]);
+        break;
+    }
+  }
+
+  /**
+   * Map the states based on the conjunctions.
+   *
+   * @param array $states
+   *   States to map.
+   *
+   * @return array
+   *   Mapped states.
+   */
+  protected static function mapStates($states) {
+    $states_new = [];
+    foreach ($states as $state_key => $value) {
+      // As the main object is ANDed together we can add the AND items directly.
+      if (!empty($states[$state_key]['AND'])) {
+        $states_new[$state_key] = $states[$state_key]['AND'];
+      }
+      // The OR and XOR groups are moved into a sub-array that has numeric keys
+      // so that we get a JSON array and not an object, as required by the States
+      // API for OR and XOR groupings.
+      if (!empty($states[$state_key]['OR'])) {
+        $or = [];
+        foreach ($states[$state_key]['OR'] as $constraint_key => $constraint_value) {
+          $or[] = [$constraint_key => $constraint_value];
+        }
+        // '1' as a string so that we get an object (which means logic groups
+        // are ANDed together).
+        $states_new[$state_key]['1'] = $or;
+      }
+      if (!empty($states[$state_key]['XOR'])) {
+        $xor = ['xor'];
+        foreach ($states[$state_key]['XOR'] as $constraint_key => $constraint_value) {
+          $xor[] = [$constraint_key => $constraint_value];
+        }
+        // '2' as a string so that we get an object.
+        $states_new[$state_key]['2'] = $xor;
+      }
+    }
+    return $states_new;
+  }
+
 }
