diff --git a/field_conditional_state.admin.inc b/field_conditional_state.admin.inc
index a238938..1e3fbaf 100644
--- a/field_conditional_state.admin.inc
+++ b/field_conditional_state.admin.inc
@@ -11,32 +11,34 @@
  *   the field type
  */
 function _field_conditional_state_get_field_states($type) {
-  $ret = array();
+  $states_available = array();
+
   switch ($type) {
     case 'list_boolean':
-      $ret = array(
-        'enabled',
-        'disabled',
-        'required',
-        'optional',
-        'visible',
-        'invisible',
-        'checked',
-        'unchecked',
+      $states_available = array(
+        'enabled' => t('Enabled'),
+        'disabled' => t('Disabled'),
+        'required' => t('Required'),
+        'optional' => t('Optional'),
+        'visible' => t('Visible'),
+        'invisible' => t('Invisible'),
+        'checked' => t('Checked'),
+        'unchecked' => t('Unchecked'),
       );
       break;
 
     default:
-      $ret = array('enabled',
-        'disabled',
-        'required',
-        'optional',
-        'visible',
-        'invisible',
+      $states_available = array(
+        'enabled' => t('Enabled'),
+        'disabled' => t('Disabled'),
+        'required' => t('Required'),
+        'optional' => t('Optional'),
+        'visible' => t('Visible'),
+        'invisible' => t('Invisible'),
       );
   }
 
-  return $ret;
+  return $states_available;
 }
 
 /**
@@ -46,28 +48,29 @@ function _field_conditional_state_get_field_states($type) {
  *   the field type
  */
 function _field_conditional_state_get_trigger_states($type) {
-  $ret = array();
+  $states_triggerable = array();
+
   switch ($type) {
     case 'list_boolean':
-      $ret = array('checked', 'unchecked');
+      $states_triggerable = array('checked', 'unchecked');
       break;
 
     case 'list_text':
     case 'list_float':
     case 'list_integer':
-      $ret = array('value');
+      $states_triggerable = array('value');
       break;
 
     case 'file':
     case 'image':
-      $ret = array('empty', 'filled');
+      $states_triggerable = array('empty', 'filled');
       break;
 
     default:
-      $ret = array('empty', 'filled', 'value');
+      $states_triggerable = array('empty', 'filled', 'value');
   }
 
-  return $ret;
+  return $states_triggerable;
 }
 
 /**
@@ -123,17 +126,17 @@ function field_conditional_state_settings_form($form, &$form_state, $instance) {
   }
 
   $subforms = array();
-  foreach ($fcs_states['possible_states'] as $state) {
+  foreach (array_keys($fcs_states['possible_states']) as $state) {
     $form['content']['tab-' . $state] = array(
       '#type' => 'fieldset',
-      '#title' => check_plain($state),
+      '#title' => $fcs_states['possible_states'][$state],
       'content' => array(),
     );
 
     $subforms[$state] = &$form['content']['tab-' . $state]['content'];
 
     $subforms[$state]['info'] = array(
-      '#markup' => "<p>" . t('The state is activated if and only if all listed groups are true.') . "</p>",
+      '#markup' => '<p>' . t('The state is activated if and only if all listed groups are true.') . '</p>',
     );
 
     $subforms[$state]['new_group'] = array(
@@ -157,8 +160,7 @@ function field_conditional_state_settings_form($form, &$form_state, $instance) {
     $new_row = &$subforms[$group['state']][$group['group_id']];
     $new_row = array(
       'states' => array(
-        '#prefix' => '<table id="' . $table_id . '">
-          <tr><th>' . t('Control field') . '</th><th>' . t('Trigger state') . '</th><th>' . t('Trigger values') . '</th><th></th></tr>',
+        '#prefix' => '<table ' . drupal_attributes(array('id' => $table_id)) . '><tr><th>' . t('Control field') . '</th><th>' . t('Trigger state') . '</th><th>' . t('Trigger value') . '</th><th>' . t('Operation') . '</th></tr>',
         '#suffix' => '</table>',
       ),
     );
@@ -295,41 +297,41 @@ function field_conditional_state_settings_form_submit($form, $form_state) {
       $fields['trigger_value'] = $values['trigger_value-' . $id];
     }
 
-    $query = db_update('field_conditional_states');
-    $query->fields($fields);
-    $query->condition('id', $id);
-    $query->execute();
+    db_update('field_conditional_state')
+      ->fields($fields)
+      ->condition('id', $id)
+      ->execute();
   }
 
   foreach ($fcs_states['existing_groups'] as $id) {
     if (!empty($values['control_field-new-' . $id]) && !empty($values['trigger_state-new-' . $id])) {
-      $query = db_insert('field_conditional_states');
-      $trigger_value = ($values['trigger_state-new-' . $id] == 'value' ? $values['trigger_value-new-' . $id] : 1);
-      $query->fields(array(
-        'group_id' => $id,
-        'control_field' => $values['control_field-new-' . $id],
-        'trigger_state' => $values['trigger_state-new-' . $id],
-        'trigger_value' => $trigger_value,
-      ));
-      $query->execute();
+      $trigger_value = ($values['trigger_state-new-' . $id] == 'value') ? $values['trigger_value-new-' . $id] : 1;
+      db_insert('field_conditional_state')
+        ->fields(array(
+          'group_id' => $id,
+          'control_field' => $values['control_field-new-' . $id],
+          'trigger_state' => $values['trigger_state-new-' . $id],
+          'trigger_value' => $trigger_value,
+        ))
+        ->execute();
     }
-    $q = db_update('field_conditional_states_groups');
-    $q->fields(array('type' => $values['group_type-' . $id]));
-    $q->condition('group_id', $id);
-    $q->execute();
+    db_update('field_conditional_states_group')
+      ->fields(array('type' => $values['group_type-' . $id]))
+      ->condition('group_id', $id)
+      ->execute();
   }
 
-  foreach ($fcs_states['possible_states'] as $state) {
+  foreach (array_keys($fcs_states['possible_states']) as $state) {
     if (!empty($values['new-group-' . $state])) {
-      $q = db_insert('field_conditional_states_groups');
-      $q->fields(array(
-        'state' => $state,
-        'entity_type' => $instance['entity_type'],
-        'bundle' => $instance['bundle'],
-        'field_name' => $instance['field_name'],
-        'type' => $values['new-group-' . $state],
-      ));
-      $q->execute();
+      db_insert('field_conditional_states_group')
+        ->fields(array(
+          'state' => $fcs_states['possible_states'][$state],
+          'entity_type' => $instance['entity_type'],
+          'bundle' => $instance['bundle'],
+          'field_name' => $instance['field_name'],
+          'type' => $values['new-group-' . $state],
+        ))
+        ->execute();
     }
   }
 }
@@ -338,10 +340,9 @@ function field_conditional_state_settings_form_submit($form, $form_state) {
  * Returns a basic / empty row of a single condition.
  */
 function _field_conditional_state_get_state_default_row($id, $classes = array()) {
-  $class_string = implode(" ", $classes);
   $state_id = drupal_html_id('field_conditional_state-state-' . $id);
   $new_state = array(
-    '#prefix' => '<tr class="' . $class_string . '" id="' . $state_id . '">',
+    '#prefix' => '<tr ' . drupal_attributes(array('id' => $state_id, 'class' => $classes)) . '>',
     '#suffix' => '</tr>',
   );
 
@@ -358,7 +359,7 @@ function _field_conditional_state_get_state_default_row($id, $classes = array())
   );
 
   $new_state['trigger_state-' . $id] = array(
-    '#prefix' => '<td id="trigger_state-' . $id . '">',
+    '#prefix' => '<td ' . drupal_attributes(array('id' => drupal_html_id('trigger_state-' . $id))) . '>',
     '#suffix' => '</td>',
     '#type' => 'select',
     '#options' => array(),
@@ -370,7 +371,7 @@ function _field_conditional_state_get_state_default_row($id, $classes = array())
     '#type' => 'textfield',
     '#states' => array(
       'visible' => array(
-        ':input[name="trigger_state-' . $id . '"]' => array('value' => 'value'),
+        ':input[name="trigger_state-' . check_plain($id) . '"]' => array('value' => 'value'),
       ),
     ),
   );
@@ -406,7 +407,7 @@ function _field_conditional_state_control_field_change($form, $form_state) {
  * Ajax callback for removing elements.
  */
 function _field_conditional_state_ajax_remove($form, $form_state) {
-  return "";
+  return '';
 }
 
 /**
@@ -415,9 +416,9 @@ function _field_conditional_state_ajax_remove($form, $form_state) {
 function _field_conditional_state_delete_condition($form, $form_state) {
   $state_id = $form_state['triggering_element']['#field_conditional_state_state_id'];
   if (!empty($state_id)) {
-    $query = db_delete('field_conditional_states');
-    $query->condition('id', $state_id);
-    $query->execute();
+    db_delete('field_conditional_state')
+      ->condition('id', $state_id)
+      ->execute();
   }
 }
 
@@ -428,12 +429,12 @@ function _field_conditional_state_delete_condition($form, $form_state) {
 function _field_conditional_state_delete_condition_group($form, $form_state) {
   $group_id = $form_state['triggering_element']['#field_conditional_state_group_id'];
   if (!empty($group_id)) {
-    $query = db_delete('field_conditional_states');
-    $query->condition('group_id', $group_id);
-    $query->execute();
+    db_delete('field_conditional_state')
+      ->condition('group_id', $group_id)
+      ->execute();
 
-    $query = db_delete('field_conditional_states_groups');
-    $query->condition('group_id', $group_id);
-    $query->execute();
+    db_delete('field_conditional_states_group')
+      ->condition('group_id', $group_id)
+      ->execute();
   }
 }
diff --git a/field_conditional_state.install b/field_conditional_state.install
index 071c5b6..683ba4a 100644
--- a/field_conditional_state.install
+++ b/field_conditional_state.install
@@ -10,17 +10,17 @@
  */
 function field_conditional_state_install() {
   // Set the module weight to 100.
-  $q = db_update('system');
-  $q->fields(array('weight' => 100));
-  $q->condition('name', 'field_conditional_state');
-  $q->execute();
+  db_update('system')
+    ->fields(array('weight' => 100))
+    ->condition('name', 'field_conditional_state')
+    ->execute();
 }
 
 /**
  * Implements hook_schema().
  */
 function field_conditional_state_schema() {
-  $schema['field_conditional_states'] = array(
+  $schema['field_conditional_state'] = array(
     'fields' => array(
       'id' => array(
         'type' => 'serial',
@@ -52,14 +52,14 @@ function field_conditional_state_schema() {
     ),
     'primary key' => array('id'),
     'foreign keys' => array(
-      'field_conditional_states_groups' => array(
-        'table' => 'field_conditional_states_groups',
+      'field_conditional_states_group' => array(
+        'table' => 'field_conditional_states_group',
         'columns' => array('group_id' => 'group_id'),
       ),
     ),
   );
 
-  $schema['field_conditional_states_groups'] = array(
+  $schema['field_conditional_states_group'] = array(
     'fields' => array(
       'group_id' => array(
         'type' => 'serial',
@@ -102,3 +102,170 @@ function field_conditional_state_schema() {
 
   return $schema;
 }
+
+/**
+ * Change the field_conditional_state.trigger_values field from varchar to text.
+ */
+function field_conditional_state_update_7100() {
+  $spec = array(
+    'type' => 'text',
+    'not null' => TRUE,
+    'description' => 'The trigger values.',
+  );
+
+  db_change_field('field_conditional_state', 'trigger_values', 'trigger_values', $spec);
+
+  return t('Changed "trigger_values" field from type "varchar" to "text".');
+}
+
+/**
+ * Upgrade module from 7.x-1.x to 7.x-2.x.
+ */
+function field_conditional_state_update_7200() {
+
+  // Add new group_id field.
+  $spec = array(
+    'type' => 'int',
+    'not null' => TRUE,
+    'initial' => 0,
+    'description' => 'The field conditional states group id.',
+  );
+  db_add_field('field_conditional_state', 'group_id', $spec);
+
+  $spec = array(
+    'type' => 'text',
+    'not null' => TRUE,
+    'initial' => '',
+    'description' => 'The trigger value.',
+  );
+  db_add_field('field_conditional_state', 'trigger_value', $spec);
+
+  $spec = array(
+    'type' => 'varchar',
+    'length' => '15',
+    'not null' => TRUE,
+    'initial' => '',
+    'description' => 'The trigger state.',
+  );
+  db_add_field('field_conditional_state', 'trigger_state', $spec);
+
+  // Change control_field field properties.
+  $spec = array(
+    'type' => 'varchar',
+    'length' => '255',
+    'not null' => TRUE,
+    'description' => 'The control field name.',
+  );
+  db_change_field('field_conditional_state', 'control_field', 'control_field', $spec);
+
+  // Add new states group table.
+  $schema['field_conditional_states_group'] = array(
+    'fields' => array(
+      'group_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+        'description' => 'The internal identifier.',
+      ),
+      'state' => array(
+        'type' => 'varchar',
+        'length' => '15',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'entity_type' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'bundle' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'field_name' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'description' => '',
+      ),
+      'type' => array(
+        'type' => 'varchar',
+        'length' => '3',
+        'not null' => TRUE,
+        'description' => 'The logical connection.',
+      ),
+    ),
+    'primary key' => array('group_id'),
+  );
+
+  db_create_table('field_conditional_states_group', $schema['field_conditional_states_group']);
+
+  $bundle_data = field_info_bundles();
+  // Upgrade settings data from 7.x-1.x to 7.x-2.x.
+  $fields = db_query("SELECT * FROM {field_conditional_state}");
+  foreach ($fields as $field) {
+    // In case FCS was used for a non-node we have to use the right entity type
+    // so we have to search for the bundle within the existing entity types
+    // if more than one entity type has a bundle with the same name
+    // we can't decide which one is the right bundle
+    // (so we'll simply take the first found entity type).
+    $entity_type = 'node';
+    foreach ($bundle_data as $type => $bundles) {
+      if (isset($bundles[$field->bundle])) {
+        $entity_type = $type;
+        break;
+      }
+    }
+
+    $group_id = db_insert('field_conditional_states_group')
+      ->fields(array(
+        'state' => $field->state,
+        'entity_type' => $entity_type,
+        'bundle' => $field->bundle,
+        'field_name' => $field->field_name,
+        'type' => 'or',
+      ))
+      ->execute();
+
+    $control_field_info = field_info_field($field->control_field);
+
+    $trigger_values = array_keys(unserialize($field->trigger_values));
+    foreach ($trigger_values as $trigger_value) {
+      $trigger_state = 'value';
+      if ($control_field_info['type'] == 'list_boolean') {
+        $trigger_state = ($trigger_value ? 'checked' : 'unchecked');
+      }
+
+      db_insert('field_conditional_state')
+      ->fields(array(
+        'group_id' => $group_id,
+        'control_field' => $field->control_field,
+        'trigger_state' => $trigger_state,
+        'trigger_value' => $trigger_value,
+        // We are going to delete the following fields later,
+        // but we have to provide a value for them because
+        // no NULL values are allowed.
+        'field_name' => '',
+        'state' => '',
+        'bundle' => '',
+        'trigger_values' => '',
+        'condition_type' => '',
+      ))
+      ->execute();
+    }
+    db_delete('field_conditional_state')
+      ->condition('id', $field->id)
+      ->execute();
+  }
+
+  // Remove no longer needed fields.
+  db_drop_field('field_conditional_state', 'field_name');
+  db_drop_field('field_conditional_state', 'state');
+  db_drop_field('field_conditional_state', 'bundle');
+  db_drop_field('field_conditional_state', 'trigger_values');
+  db_drop_field('field_conditional_state', 'condition_type');
+
+  return t('Upgraded module settings from 7.x-1.x to 7.x-2.x.');
+}
diff --git a/field_conditional_state.module b/field_conditional_state.module
index fa575e2..eab68d6 100644
--- a/field_conditional_state.module
+++ b/field_conditional_state.module
@@ -28,12 +28,12 @@ function _field_conditional_state_get_conditional_states($entity_type, $bundle,
     return $records[$entity_type][$bundle][$field_name];
   }
   else {
-    $query = db_select('field_conditional_states_groups', 'g');
-    $query->fields('g');
-    $query->condition('entity_type', $entity_type);
-    $query->condition('bundle', $bundle);
-    $query->condition('field_name', $field_name);
-    $groups = $query->execute();
+    $groups = db_select('field_conditional_states_group', 'g')
+      ->fields('g')
+      ->condition('entity_type', $entity_type)
+      ->condition('bundle', $bundle)
+      ->condition('field_name', $field_name)
+      ->execute();
 
     $data = array();
     while ($group = $groups->fetchAssoc()) {
@@ -41,10 +41,12 @@ function _field_conditional_state_get_conditional_states($entity_type, $bundle,
       $tmp = $group;
       $tmp['states'] = array();
 
-      $query = db_select('field_conditional_states', 's');
-      $query->fields('s');
-      $query->condition('group_id', $group['group_id']);
-      $states = $query->execute();
+      $states = db_select('field_conditional_state', 's')
+        ->fields('s')
+        ->condition('group_id', $group['group_id'])
+        ->orderBy('control_field', 'ASC')
+        ->execute();
+
       while ($state = $states->fetchAssoc()) {
         $tmp['states'][] = $state;
       }
@@ -105,15 +107,14 @@ function _field_conditional_state_get_field_suffix($element) {
  */
 function field_conditional_state_element_process($element, &$form_state, &$form) {
   if (isset($element['#entity_type']) && isset($element['#bundle']) && isset($element['#field_name'])) {
-    
     /*
-     * An array containing the IDs generated by drupal_html_id 
+     * An array containing the IDs generated by drupal_html_id
      * keyed by their original form (as the argument of drupal_html_id)
      */
     $ids = &drupal_static(__FUNCTION__, array());
-    
+
     $element['#field_conditional_state_id'] = _field_conditional_state_get_field_suffix($element);
-    
+
     if (empty($element['#pre_render'])) {
       $element['#pre_render'] = array();
     }
@@ -121,14 +122,19 @@ function field_conditional_state_element_process($element, &$form_state, &$form)
 
     $original_id = $element['#field_name'] . '_field_conditional_state_control_' . $element['#field_conditional_state_id'];
     $ids[$original_id] = drupal_html_id($original_id);
-    $element['#prefix'] = '<div id="' . $ids[$original_id] . '">';
+    $element['#prefix'] = '<div ' . drupal_attributes(array('id' => $ids[$original_id])) . '>';
     $element['#suffix'] = "</div>";
-    #$form_state['#field_conditional_state_parents'][$element['#field_name'] . $element['#field_conditional_state_id']] = $element['#parents'];
 
   }
   return $element;
 }
 
+/**
+ * Pre-render callback.
+ *
+ * Adds the #states to the element and wraps it
+ * into another element if necessary.
+ */
 function _field_conditional_state_add_states($element) {
   $states = _field_conditional_state_get_conditional_states($element['#entity_type'], $element['#bundle'], $element['#field_name']);
   _field_conditional_state_build_states_array($element, $states);
@@ -137,8 +143,8 @@ function _field_conditional_state_add_states($element) {
   if ($is_dependent) {
     if (empty($element['#type'])) {
       // Adds a container with the given ID to elements that are dependent
-      // and where the ID would otherwise not be rendered
-      $element['#prefix'] = '<div id="' . $element['#id'] . '">';
+      // and where the ID would otherwise not be rendered.
+      $element['#prefix'] = '<div ' . drupal_attributes(array('id' => $element['#id'])) . '>';
       $element['#suffix'] = '</div>';
     }
   }
@@ -246,7 +252,7 @@ function field_conditional_state_form_field_ui_field_overview_form_alter(&$form,
   foreach (element_children($table) as $key) {
     if (array_key_exists($key, $instances)) {
       $admin_field_path = $admin_path . '/fields/' . $instances[$key]['field_name'];
-      $table[$key]['field_conditional_states'] = array(
+      $table[$key]['field_conditional_state'] = array(
         '#type' => 'link',
         '#title' => t('States'),
         '#href' => $admin_field_path . '/field_conditional_state',
@@ -254,7 +260,7 @@ function field_conditional_state_form_field_ui_field_overview_form_alter(&$form,
       );
     }
     else {
-      $table[$key]['field_conditional_states'] = array('#markup' => '');
+      $table[$key]['field_conditional_state'] = array('#markup' => '');
     }
   }
 }
@@ -279,7 +285,7 @@ function field_conditional_state_field_widget_form_alter(&$element, &$form_state
 function _field_conditional_state_build_states_array(&$element, $groups) {
   if (count($groups) > 0) {
 
-    // see description in field_conditional_state_element_process
+    // See description in field_conditional_state_element_process.
     $ids = &drupal_static('field_conditional_state_element_process');
 
     $element['#states'] = array();
@@ -340,29 +346,25 @@ function _field_conditional_state_build_states_array(&$element, $groups) {
  * Implements hook_field_delete_instance().
  */
 function field_conditional_state_field_delete_instance($instance) {
-  $args = array(
-    ':type' => $instance['entity_type'],
-    ':bundle' => $instance['bundle'],
-  );
-  $q = db_query("SELECT group_id, field_name FROM {field_conditional_states_groups} WHERE entity_type = :type AND bundle = :bundle", $args);
+  $query = db_query("SELECT group_id, field_name FROM {field_conditional_states_groups} WHERE entity_type = :type AND bundle = :bundle", array(':type' => $instance['entity_type'], ':bundle' => $instance['bundle']));
 
-  while ($record = $q->fetchAssoc()) {
-    $del = db_delete('field_conditional_states');
-    $del->condition('group_id', $record['group_id']);
+  while ($record = $query->fetchAssoc()) {
+    $delete = db_delete('field_conditional_state');
+    $delete->condition('group_id', $record['group_id']);
 
     // If the current groups field name is equal to the deleted field name
     // delete the group and ALL associated states
     // otherwise delete only the conditions
     // where the deleted field is the control field.
     if ($record['field_name'] == $instance['field_name']) {
-      $del_group = db_delete('field_conditional_states_groups');
-      $del_group->condition('group_id', $record['group_id']);
-      $del_group->execute();
+      db_delete('field_conditional_states_group')
+        ->condition('group_id', $record['group_id'])
+        ->execute();
     }
     else {
-      $del->condition('control_field', $record['field_name']);
+      $delete->condition('control_field', $record['field_name']);
     }
 
-    $del->execute();
+    $delete->execute();
   }
 }
