diff --git a/term_merge.module b/term_merge.module
index 2c44838..f864cac 100644
--- a/term_merge.module
+++ b/term_merge.module
@@ -318,20 +318,35 @@ function term_merge_action($object, $context) {
     // @todo: Here would be nice to throw in a hook, allowing other modules to
     // supply meta data about their field types if they also use taxonomy
     // references, defining it in their own field types.
-    if ($v['type'] == 'taxonomy_term_reference') {
+
+    $field_info = field_read_field($field_name);
+    $v['foreign keys'] = $field_info['foreign keys'];
+
+    // Find table and column.
+    $key = key($v['foreign keys']);
+    $table = !empty($v['foreign keys'][$key]['table'])?$v['foreign keys'][$key]['table']:'';
+    $column = !empty($v['foreign keys'][$key]['columns'])?key($v['foreign keys'][$key]['columns']):'';
+    // Build field list with entities and columns.
+    if ($table == 'taxonomy_term_data') {
       $result[$field_name] = array();
       $query = new EntityFieldQuery();
       // Making sure we search in the entire scope of entities.
       $query->addMetaData('account', user_load(1));
-      $query->fieldCondition($field_name, 'tid', $term_branch->tid);
+
+      $query->fieldCondition($field_name, $column, $term_branch->tid);
       $_result = $query->execute();
-      $result[$field_name] = array_merge_recursive($result[$field_name], $_result);
+      // Add entities.
+      $result[$field_name]['entities'] = array_merge_recursive($result[$field_name], $_result);
+      // Add column.
+      $result[$field_name]['column'] = $column;
     }
   }
   // Now we load all entities that have taxonomy_term_reference pointing to
   // $term_branch.
-  foreach ($result as $field_name => $entity_types) {
-    foreach ($entity_types as $entity_type => $v) {
+  foreach ($result as $field_name => $field_data) {
+    // Find column for current field.
+    $column = $field_data['column'];
+    foreach ($field_data['entities'] as $entity_type => $v) {
       $ids = array_keys($v);
       $entities = entity_load($entity_type, $ids);
       // After we have loaded it, we alter the taxonomy_term_reference
@@ -344,7 +359,7 @@ function term_merge_action($object, $context) {
           // 'keep_only_unique'.
           $is_trunk_added = FALSE;
           foreach ($entity->{$field_name}[$language] as $delta => $item) {
-            if ($context['keep_only_unique'] && $is_trunk_added && in_array($item['tid'], array($term_trunk->tid, $term_branch->tid))) {
+            if ($context['keep_only_unique'] && $is_trunk_added && in_array($item[$column], array($term_trunk->tid, $term_branch->tid))) {
               // We are instructed to keep only unique references and we already
               // have term trunk in this field, so we just unset value for this
               // delta.
@@ -353,14 +368,14 @@ function term_merge_action($object, $context) {
             else {
               // Merging term references if necessary, and keep an eye on
               // whether we already have term trunk among this field values.
-              switch ($item['tid']) {
+              switch ($item[$column]) {
                 case $term_trunk->tid:
                   $is_trunk_added = TRUE;
                   break;
 
                 case $term_branch->tid:
                   $is_trunk_added = TRUE;
-                  $entity->{$field_name}[$language][$delta]['tid'] = $term_trunk->tid;
+                  $entity->{$field_name}[$language][$delta][$column] = $term_trunk->tid;
                   break;
               }
             }
