diff --git API.txt API.txt
index c8acfea..151c04f 100644
--- API.txt
+++ API.txt
@@ -24,7 +24,7 @@ function hook_flexifilter() {
 A description of hook_flexifilter_components:
 
 /**
- * Implementation of hook_flexifilter_components()
+ * Implements hook_flexifilter_components().
  *
  * @return An array of components to be used by the Flexifilter module. The keys of this array
  * are unique identifiers for the component (called the component class), and the values of the
diff --git flexifilter.admin.inc flexifilter.admin.inc
index 9023be4..1761d83 100644
--- flexifilter.admin.inc
+++ flexifilter.admin.inc
@@ -2,23 +2,28 @@
 // $Id: flexifilter.admin.inc,v 1.14.2.3 2008/11/22 22:13:33 cwgordon7 Exp $
 
 /**
+ * @file
+ * Contains administrative pages for the Flexifield module.
+ */
+
+/**
  * Generates the form for the admin overview page
  */
-function flexifilter_filter_list_form() {
-  drupal_set_title("Flexifilters overview");
-  $path = drupal_get_path('module', 'flexifilter') .'/flexifilter.css';
-  drupal_add_css($path, 'module', 'all', FALSE);
+function flexifilter_filter_list_form($form, &$form_state) {
+  drupal_set_title(t('Flexifilters overview'));
+  $path = drupal_get_path('module', 'flexifilter') . '/flexifilter.css';
+  drupal_add_css($path, array('preprocess' => FALSE));
 
   $form = array();
   $form['enabled_header'] = array(
-    '#value' => '<h3>'. t('Enabled Flexifilters') .'</h3>',
-    '#suffix' => t('If a flexifilter is enabled, then it can be used within input formats, however there can only be 128 enabled at any one time.') .'<br/>',
+    '#markup' => '<h3>' . t('Enabled Flexifilters') . '</h3>',
+    '#suffix' => t('If a flexifilter is enabled, then it can be used within input formats, however there can only be 128 enabled at any one time.') . '<br/>',
   );
   $form['enabled'] = flexifilter_filter_list_form_table(TRUE);
 
   $form['disabled_header'] = array(
-    '#value' => '<h3>'. t('Disabled Flexifilters') .'</h3>',
-    '#suffix' => t('If a flexifilter is disabled, then it cannot be used within input formats, although it can be used within other flexifilters.') .'<br/>',
+    '#markup' => '<h3>' . t('Disabled Flexifilters') . '</h3>',
+    '#suffix' => t('If a flexifilter is disabled, then it cannot be used within input formats, although it can be used within other flexifilters.') . '<br/>',
   );
   $form['disabled'] = flexifilter_filter_list_form_table(FALSE);
 
@@ -32,34 +37,29 @@ function flexifilter_filter_list_form() {
  *   Is TRUE if we're theming the currently enabled filters.s
  */
 function flexifilter_filter_list_form_table($enabled) {
-  $show_disable_link = $enabled;
-  $show_enable_link  = !$enabled && flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS;
   $header            = array(t('Label'), t('Description'), t('Edit'), t('Delete'), t('Preview'), t($enabled ? 'Disable' : 'Enable'));
   $rows              = array();
   $filters           = flexifilter_get_filters();
   foreach ($filters as $id => $filter) {
     if ($filter['enabled'] == $enabled) {
       $row   = array($filter['label'], $filter['description']);
-      $row[] = l(t('Edit'), 'admin/build/flexifilters/'. $id .'/edit', array(), drupal_get_destination());
-      $row[] = l(t('Delete'), 'admin/build/flexifilters/'. $id .'/delete', array(), drupal_get_destination());
-      $row[] = l(t('Preview'), 'admin/build/flexifilters/'. $id .'/preview', array(), drupal_get_destination());  
-      if ($show_disable_link) {
-        $row[] = l(t('Disable'), 'admin/build/flexifilters/'. $id .'/disable', array(), drupal_get_destination());
-      }
-      else if ($show_enable_link) {
-        $row[] = l(t('Enable'), 'admin/build/flexifilters/'. $id .'/enable', array(), drupal_get_destination());
+      $row[] = l(t('Edit'), 'admin/structure/flexifilters/' . $id . '/edit', array(), drupal_get_destination());
+      $row[] = l(t('Delete'), 'admin/structure/flexifilters/' . $id . '/delete', array(), drupal_get_destination());
+      $row[] = l(t('Preview'), 'admin/structure/flexifilters/' . $id . '/preview', array(), drupal_get_destination());
+      if ($enabled) {
+        $row[] = l(t('Disable'), 'admin/structure/flexifilters/' . $id . '/disable', array(), drupal_get_destination());
       }
       else {
-        $row[] = '';
+        $row[] = l(t('Enable'), 'admin/structure/flexifilters/' . $id . '/enable', array(), drupal_get_destination());
       }
       $rows[] = $row;
     }
   }
   if (count($rows)) {
-    return array('#value' => theme('table', $header, $rows, array('class' => 'flexifilters-list')));
+    return array('#markup' => theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('flexifilters-list')))));
   }
   else {
-    return array('#value' => t('None.'));
+    return array('#markup' => t('None.'));
   }
 }
 
@@ -83,10 +83,10 @@ function flexifilter_undo_submit_default_name($form) {
     // To make it easier to handle $_FILES in file.inc, we place all
     // file fields in the 'files' array. Also, we do not support
     // nested file names.
-    $form['#name'] = 'files['. $form['#name'] .']';
+    $form['#name'] = 'files[' . $form['#name'] . ']';
   }
   elseif (count($form['#parents'])) {
-    $form['#name'] .= '['. implode('][', $form['#parents']) .']';
+    $form['#name'] .= '[' . implode('][', $form['#parents']) . ']';
   }
   array_unshift($form['#parents'], $name);
   return $form;
@@ -103,11 +103,11 @@ function flexifilter_filter_edit_condition_data($data) {
   foreach ($data['settings'] as $key => $value) {
     if ($key == 'conditions') {
       foreach ($value as $cond_key => $cond_value) {
-        $condition['condition_'. $cond_key] = flexifilter_filter_edit_condition_data($cond_value);
+        $condition['condition_' . $cond_key] = flexifilter_filter_edit_condition_data($cond_value);
       }
     }
     else {
-      $condition['setting_'. $key] = $value;
+      $condition['setting_' . $key] = $value;
     }
   }
   return $condition;
@@ -141,7 +141,7 @@ function flexifilter_filter_edit_components_data($components, $id_prefix = NULL)
         $us[$key] = $value;
       }
       else {
-        $us['setting_'. $key] = $value;
+        $us['setting_' . $key] = $value;
       }
     }
     $us['weight'] = $n;
@@ -165,7 +165,7 @@ function flexifilter_filter_edit_form_new_component_data($component_class) {
   $data       = array(
     'class' => $component_class,
     'weight' => 10000,
-    'is_new' => true,
+    'is_new' => TRUE,
   );
   if ($component['step'] == 'either') {
     $data['step'] = 'process';
@@ -211,7 +211,10 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
       '#title' => t('No condition'),
       '#description' => t('There is no condition set. This will probably cause the component to do nothing, as "No Condition" evaluates to false.'),
     );
-    $form['class'] = array('#type' => 'hidden', '#value' => '');
+    $form['class'] = array(
+      '#type' => 'hidden',
+      '#value' => '',
+    );
     return $form;
   }
 
@@ -225,7 +228,10 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
     '#title' => $condition['label'],
     '#description' => $condition['description'],
   );
-  $form['class'] = array('#type' => 'hidden', '#value' => $data['class']);
+  $form['class'] = array(
+    '#type' => 'hidden',
+    '#value' => $data['class'],
+  );
   $custom_settings = array();
   foreach ($data as $key => $value) {
     if (strncmp($key, 'setting_', 8) == 0) {
@@ -235,7 +241,7 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
   $custom_elements = flexifilter_invoke_condition(array('class' => $data['class'], 'settings' => $custom_settings), 'settings');
   if (is_array($custom_elements)) {
     foreach ($custom_elements as $key => $element) {
-      $form['setting_'. $key] = $element;
+      $form['setting_' . $key] = $element;
     }
   }
   if ($condition['is_container']) {
@@ -245,7 +251,7 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
       $condition_max_id = 0;
       foreach ($data as $key => $sub_condition) {
         if (strncmp($key, 'condition_', 10) == 0) {
-          $condition_max_id = max($condition_max_id, 1 + (int)substr($key, 10));
+          $condition_max_id = max($condition_max_id, 1 + (int) substr($key, 10));
           if (!isset($sub_condition['op'])) {
             $form[$key] = flexifilter_filter_edit_form_condition($sub_condition, $show_advanced);
           }
@@ -255,7 +261,7 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
         }
       }
       if (isset($data['op_c'])) {
-        $form['condition_'. $condition_max_id] = flexifilter_filter_edit_form_condition(flexifilter_filter_edit_form_new_condition_data($data['add_condition']), $show_advanced);
+        $form['condition_' . $condition_max_id] = flexifilter_filter_edit_form_condition(flexifilter_filter_edit_form_new_condition_data($data['add_condition']), $show_advanced);
       }
 
       $form['add_condition'] = array(
@@ -267,17 +273,20 @@ function flexifilter_filter_edit_form_condition($data, $show_advanced) {
       $form['op_c'] = array(
         '#type' => 'submit',
         '#value' => t('Add'),
-        '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
+        '#process' => array('flexifilter_undo_submit_default_name'),
       );
       // Add some kind of seperator, otherwise this add button and the remove button are put onto the same line
       // which is just a little bit confusing to the user. Hence we use a horizontal rule to seperate them alot.
-      $form['end_add_cond'] = array('#type' => 'markup', '#value' => '<br/><br/><hr/><br/>');
+      $form['end_add_cond'] = array(
+        '#type' => 'markup',
+        '#value' => '<br/><br/><hr/><br/>',
+      );
     }
   }
   $form['op'] = array(
     '#type' => 'submit',
     '#value' => t('Remove condition'),
-    '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
+    '#process' => array('flexifilter_undo_submit_default_name'),
   );
 
   return $form;
@@ -315,7 +324,7 @@ function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $
   $custom_elements = flexifilter_invoke_component($component, 'settings', $custom_settings);
   if (is_array($custom_elements)) {
     foreach ($custom_elements as $key => $element) {
-      $form['setting_'. $key] = $element;
+      $form['setting_' . $key] = $element;
     }
   }
   if ($component['is_container']) {
@@ -343,7 +352,7 @@ function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $
       $form['op_c'] = array(
         '#type' => 'submit',
         '#value' => t('Change'),
-        '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
+        '#process' => array('flexifilter_undo_submit_default_name'),
       );
     }
     if ($component['contains_components']) {
@@ -379,7 +388,7 @@ function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $
     '#options' => array(),
     // If the element at the bottom is moved to the top, then the #value of 'top' will no longer be in the downdown
     // and the FAPI throws a nasty validation error. Hence mark it already validated and we will do our own validation.
-    '#validated' => true,
+    '#validated' => TRUE,
   );
   if (!$is_first) {
     $form['move']['#options']['top'] = t('Move to top');
@@ -393,7 +402,7 @@ function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $
   $form['op'] = array(
     '#type' => 'submit',
     '#value' => t('Re/move'),
-    '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
+    '#process' => array('flexifilter_undo_submit_default_name'),
   );
 
   return $form;
@@ -403,7 +412,7 @@ function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $
  * Helper function; sorts components by weight.
  */
 function _flexifilter_components_sort_weight($a, $b) {
-  return ((float)$a['weight']) > ((float)$b['weight']);
+  return ((float) $a['weight']) > ((float) $b['weight']);
 }
 
 /**
@@ -423,7 +432,7 @@ function flexifilter_filter_edit_form_components($data, &$data_base, $show_advan
   );
   $components = array();
   foreach ($data as $key => $component) {
-    if (strncmp($key, $data_base['id_prefix'], strlen($data_base['id_prefix'])) == 0) {
+    if (strncmp($key, $data_base['id_prefix'], drupal_strlen($data_base['id_prefix'])) == 0) {
       $components[$key] = $component;
     }
   }
@@ -435,10 +444,10 @@ function flexifilter_filter_edit_form_components($data, &$data_base, $show_advan
     if (isset($component['op'])) {
       switch ($component['move']) {
         /*
-          Careful; this component is NOT VALIDATED by the FAPI. We only act upon certain
-          values ('top', 'up', 'down', 'bottom', 'remove'), which are always valid (even
-          if they have no effect). Other values are silently ignored.
-        */
+         Careful; this component is NOT VALIDATED by the FAPI. We only act upon certain
+         values ('top', 'up', 'down', 'bottom', 'remove'), which are always valid (even
+         if they have no effect). Other values are silently ignored.
+         */
 
         case 'top':
           $components[$key]['weight'] = -1000;
@@ -481,7 +490,7 @@ function flexifilter_filter_edit_form_components($data, &$data_base, $show_advan
   $form['op'] = array(
     '#type' => 'submit',
     '#value' => t('Add'),
-    '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
+    '#process' => array('flexifilter_undo_submit_default_name'),
   );
   return $form;
 }
@@ -489,7 +498,7 @@ function flexifilter_filter_edit_form_components($data, &$data_base, $show_advan
 /**
  * The add/edit form for flexifilters.
  */
-function flexifilter_filter_edit_form($form_state, $flexifilter = 'new') {
+function flexifilter_filter_edit_form($form, &$form_state, $flexifilter = 'new') {
   if (!isset($form_state['post']) || !isset($form_state['post']['fid'])) {
     $data = array();
     $data['fid'] = $flexifilter === 'new' ? 'new' : $flexifilter['id'];
@@ -500,7 +509,7 @@ function flexifilter_filter_edit_form($form_state, $flexifilter = 'new') {
     $data['basic']['cache'] = $flexifilter === 'new' ? 1 : $flexifilter['cache'];
     $data['components'] = $flexifilter === 'new' ? array() : flexifilter_filter_edit_components_data($flexifilter['components']);
     if (!isset($data['components']['id_prefix']) || $data['components']['id_prefix'] === 'component_new_') {
-      $data['components']['id_prefix'] = 'component_'. $data['fid'] .'_';
+      $data['components']['id_prefix'] = 'component_' . $data['fid'] . '_';
     }
     if (!isset($data['components']['id_next'])) {
       $data['components']['id_next'] = 0;
@@ -560,8 +569,14 @@ function flexifilter_filter_edit_form($form_state, $flexifilter = 'new') {
     );
   }
   $form['components'] = flexifilter_filter_edit_form_components($data['components'], $data['components'], $data['basic']['advanced'] == 1);
-  $form['components']['id_prefix'] = array('#type' => 'hidden', '#value' => $data['components']['id_prefix']);
-  $form['components']['id_next'] = array('#type' => 'hidden', '#value' => $data['components']['id_next']);
+  $form['components']['id_prefix'] = array(
+    '#type' => 'hidden',
+    '#value' => $data['components']['id_prefix'],
+  );
+  $form['components']['id_next'] = array(
+    '#type' => 'hidden',
+    '#value' => $data['components']['id_next'],
+  );
   if (isset($expand_root_levels) && $expand_root_levels) {
     $form['basic']['#collapsed'] = FALSE;
     $form['components']['#collapsed'] = FALSE;
@@ -612,7 +627,7 @@ function flexifilter_filter_edit_form_components_from_data($data, $data_base = N
     $components['id_next'] = $data_base['id_next'];
   }
   foreach ($data as $key => $data_us) {
-    if (strncmp($key, $data_base['id_prefix'], strlen($data_base['id_prefix'])) == 0) {
+    if (strncmp($key, $data_base['id_prefix'], drupal_strlen($data_base['id_prefix'])) == 0) {
       $component = array(
         'class' => $data_us['class'],
         'settings' => array(),
@@ -640,26 +655,54 @@ function flexifilter_filter_edit_form_components_from_data($data, $data_base = N
 function flexifilter_filter_edit_form_submit_components($data, $fid, &$pids_to_reuse, $parent = FALSE) {
   if ($parent === FALSE) {
     if ($reuse_pid = array_shift($pids_to_reuse)) {
-      db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d",
-        $fid, FLEXIFILTER_PART_TYPE_ROOT, $reuse_pid);
+      db_update('flexifilters_parts')
+  ->fields(array(
+        'fid' => $fid,
+        'parent_pid' => 0,
+        'type' => FLEXIFILTER_PART_TYPE_ROOT,
+        'class_name' => '',
+        'settings' => '',
+      ))
+  ->condition('pid', $reuse_pid)
+  ->execute();
       $parent = $reuse_pid;
     }
     else {
-      db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, 0, %d, '', '')",
-        $fid, FLEXIFILTER_PART_TYPE_ROOT);
-      $parent = db_last_insert_id('flexifilters_parts', 'pid');
+      $parent = db_insert('flexifilters_parts')
+  ->fields(array(
+        'fid' => $fid,
+        'parent_pid' => 0,
+        'type' => FLEXIFILTER_PART_TYPE_ROOT,
+        'class_name' => '',
+        'settings' => '',
+      ))
+  ->execute();
     }
   }
   foreach ($data as $key => $child) {
     if (is_numeric($key)) {
       if ($child_cid = array_shift($pids_to_reuse)) {
-        db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child_cid);
+        db_update('flexifilters_parts')
+  ->fields(array(
+          'fid' => $fid,
+          'parent_pid' => $parent,
+          'type' => FLEXIFILTER_PART_TYPE_COMPONENT,
+          'class_name' => $child['class'],
+          'settings' => serialize($child['settings']),
+        ))
+  ->condition('pid', $child_cid)
+  ->execute();
       }
       else {
-        db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']));
-        $child_cid = db_last_insert_id('flexifilters_parts', 'pid');
+        $child_cid = db_insert('flexifilters_parts')
+  ->fields(array(
+          'fid' => $fid,
+          'parent_pid' => $parent,
+          'type' => FLEXIFILTER_PART_TYPE_COMPONENT,
+          'class_name' => $child['class'],
+          'settings' => serialize($child['settings']),
+        ))
+  ->execute();
       }
       if (isset($child['components'])) {
         flexifilter_filter_edit_form_submit_components($child['components'], $fid, $pids_to_reuse, $child_cid);
@@ -674,13 +717,27 @@ function flexifilter_filter_edit_form_submit_components($data, $fid, &$pids_to_r
 
 function flexifilter_filter_edit_form_submit_condition($data, $fid, &$pids_to_reuse, $parent) {
   if ($our_pid = array_shift($pids_to_reuse)) {
-    db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $data['class'], serialize($data['settings']), $our_pid);
+    db_update('flexifilters_parts')
+  ->fields(array(
+      'fid' => $fid,
+      'parent_pid' => $parent,
+      'type' => FLEXIFILTER_PART_TYPE_CONDITION,
+      'class_name' => $data['class'],
+      'settings' => serialize($data['settings']),
+    ))
+  ->condition('pid', $our_pid)
+  ->execute();
   }
   else {
-    db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $data['class'], serialize($data['settings']));
-    $our_pid = db_last_insert_id('flexifilters_parts', 'pid');
+    $our_pid = db_insert('flexifilters_parts')
+  ->fields(array(
+      'fid' => $fid,
+      'parent_pid' => $parent,
+      'type' => FLEXIFILTER_PART_TYPE_CONDITION,
+      'class_name' => $data['class'],
+      'settings' => serialize($data['settings']),
+    ))
+  ->execute();
   }
   if (isset($data['conditions'])) {
     foreach ($data['conditions'] as $condition) {
@@ -707,7 +764,7 @@ function flexifilter_filter_edit_form_submit($form, &$form_state) {
     if ($values['op'] == $values['submit2']) {
       unset($form_state['storage']);
       unset($form_state['rebuild']);
-      $form_state['redirect'] = 'admin/build/flexifilters/'. $fid .'/edit';
+      $form_state['redirect'] = 'admin/structure/flexifilters/' . $fid . '/edit';
     }
   }
   else {
@@ -716,19 +773,25 @@ function flexifilter_filter_edit_form_submit($form, &$form_state) {
   if ($values['op'] == $values['submit1']) {
     unset($form_state['storage']);
     unset($form_state['rebuild']);
-    $form_state['redirect'] = 'admin/build/flexifilters';
+    $form_state['redirect'] = 'admin/structure/flexifilters';
   }
 }
 
-function flexifilter_filter_delete_form($form_state, $flexifilter) {
+function flexifilter_filter_delete_form($form, &$form_state, $flexifilter) {
   $form = array();
-  $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
+  $form['fid'] = array(
+    '#type' => 'hidden',
+    '#value' => $flexifilter['id'],
+  );
 
-  return confirm_form($form, t('Are you sure you want to delete the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
+  return confirm_form($form, t('Are you sure you want to delete the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/structure/flexifilters', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
 }
 
-function flexifilter_remove_from_filters($delta) {
-  db_query("DELETE FROM {filters} WHERE module = 'flexifilter' AND delta = %d", $delta);
+function flexifilter_remove_from_filters($name) {
+  db_delete('filter')
+  ->condition('module', 'flexifilter')
+  ->condition('name', $name)
+  ->execute();
   cache_clear_all('*', 'cache_filter', TRUE);
 }
 
@@ -737,19 +800,26 @@ function flexifilter_filter_delete_form_submit($form, &$form_state) {
   $filters = flexifilter_get_filters();
   $filter  = $filters[$fid];
   if ($filter['enabled']) {
-    flexifilter_remove_from_filters($filter['delta']);
+    flexifilter_remove_from_filters($filter['label']);
   }
-  db_query('DELETE FROM {flexifilters_parts} WHERE fid = %d', $fid);
-  db_query('DELETE FROM {flexifilters} WHERE fid = %d', $fid);
+  db_delete('flexifilters_parts')
+  ->condition('fid', $fid)
+  ->execute();
+  db_delete('flexifilters')
+  ->condition('fid', $fid)
+  ->execute();
   drupal_set_message(t('Flexifilter deleted.'));
-  $form_state['redirect'] = 'admin/build/flexifilters';
+  $form_state['redirect'] = 'admin/structure/flexifilters';
 }
 
-function flexifilter_filter_disable_form($form_state, $flexifilter) {
+function flexifilter_filter_disable_form($form, &$form_state, $flexifilter) {
   $form = array();
-  $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
+  $form['fid'] = array(
+    '#type' => 'hidden',
+    '#value' => $flexifilter['id'],
+  );
 
-  return confirm_form($form, t('Are you sure you want to disable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t('If you disable the flexifilter, then it cannot be used in any Input Formats.'), t('Disable'), t('Cancel'));
+  return confirm_form($form, t('Are you sure you want to disable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/structure/flexifilters', t('If you disable the flexifilter, then it cannot be used in any Input Formats.'), t('Disable'), t('Cancel'));
 }
 
 function flexifilter_filter_disable_form_submit($form, &$form_state) {
@@ -757,32 +827,45 @@ function flexifilter_filter_disable_form_submit($form, &$form_state) {
   $filters = flexifilter_get_filters();
   $filter  = $filters[$fid];
   if ($filter['enabled']) {
-    flexifilter_remove_from_filters($filter['delta']);
-    db_query('UPDATE {flexifilters} SET enabled = 0 WHERE fid = %d', $fid);
+    flexifilter_remove_from_filters($filter['label']);
+    db_update('flexifilters')
+  ->fields(array(
+      'enabled' => 0,
+    ))
+  ->condition('fid', $fid)
+  ->execute();
     drupal_set_message(t('Flexifilter disabled.'));
   }
-  $form_state['redirect'] = 'admin/build/flexifilters';
+  $form_state['redirect'] = 'admin/structure/flexifilters';
 }
 
-function flexifilter_filter_enable_form($form_state, $flexifilter) {
+function flexifilter_filter_enable_form($form, &$form_state, $flexifilter) {
   $form = array();
-  $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
+  $form['fid'] = array(
+    '#type' => 'hidden',
+    '#value' => $flexifilter['id'],
+  );
 
-  return confirm_form($form, t('Are you sure you want to enable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t(''), t('Enable'), t('Cancel'));
+  return confirm_form($form, t('Are you sure you want to enable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/structure/flexifilters', t(''), t('Enable'), t('Cancel'));
 }
 
 function flexifilter_filter_enable_form_submit($form, &$form_state) {
   $fid     = $form_state['values']['fid'];
   $filters = flexifilter_get_filters();
   $filter  = $filters[$fid];
-  if (!$filter['enabled'] && flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS) {
-    db_query('UPDATE {flexifilters} SET enabled = 1, delta = %d WHERE fid = %d', flexifilter_get_unused_delta(), $fid);
+  if (!$filter['enabled']) {
+    db_update('flexifilters')
+  ->fields(array(
+      'enabled' => 1,
+    ))
+  ->condition('fid', $fid)
+  ->execute();
     drupal_set_message(t('Flexifilter enabled.'));
   }
-  $form_state['redirect'] = 'admin/build/flexifilters';
+  $form_state['redirect'] = 'admin/structure/flexifilters';
 }
 
-function flexifilter_filter_export_form($form_state, $flexifilter) {
+function flexifilter_filter_export_form($form, &$form_state, $flexifilter) {
   $form = array();
   $flexifilter['fid'] = 'new';
   $form['serialized'] = array(
@@ -818,10 +901,11 @@ function flexifilter_filter_input_form($form_state) {
   return $form;
 }
 
-function flexifilter_filter_input_form_submit($form, &$form_state) { 
-  $fid = unserialize($form_state['values']['flexifilter']);
+function flexifilter_filter_input_form_submit($form, &$form_state) {
+  $fid = unserialize(base64_decode($form_state['values']['flexifilter']));
+  dvm(base64_decode($form_state['values']['flexifilter']));
   if ($fid) {
-    drupal_goto('admin/build/flexifilters/'. flexifilter_save_filter($form_state['values']['flexifilter']) .'/edit');
+    drupal_goto('admin/structure/flexifilters/' . flexifilter_save_filter($form_state['values']['flexifilter']) . '/edit');
   }
   else {
     drupal_set_message(t('It appears that the serialized flexifilter you entered was improperly formed. Please check the source.'), 'error');
@@ -863,7 +947,7 @@ function flexifilter_filter_default_form_submit($form, &$form_state) {
     }
   }
   flexifilter_install_flexifilters('flexifilter', $to_be_saved);
-  $form_state['redirect'] = 'admin/build/flexifilters';
+  $form_state['redirect'] = 'admin/structure/flexifilters';
 }
 
 function flexifilter_filter_preview_form($form_state, $flexifilter) {
@@ -878,7 +962,7 @@ function flexifilter_filter_preview_form($form_state, $flexifilter) {
       '#value' => $flexifilter,
     );
     $form['submit'] = array(
-     '#type' => 'submit',
+      '#type' => 'submit',
       '#value' => t('Preview'),
     );
   }
@@ -907,25 +991,25 @@ function flexifilter_filter_preview_form($form_state, $flexifilter) {
       '#value' => $flexifilter,
     );
     $zebra = 'flexifilter-even';
-    drupal_add_css(drupal_get_path('module', 'flexifilter') .'/flexifilter.css');
+    drupal_add_css(drupal_get_path('module', 'flexifilter') . '/flexifilter.css');
     foreach ($form_state['storage']['data'] as $key => $data) {
       $settings = '';
       foreach ($data['settings'] as $key => $value) {
-        $settings .= '<strong>'. check_plain($key) .'</strong>: '. check_plain(''. $value) ."<br />";
+        $settings .= '<strong>' . check_plain($key) . '</strong>: ' . check_plain('' . $value) . "<br />";
       }
       $uniqid = uniqid();
       $form[$data['step']][$key . $uniqid]['step'] = array(
         '#type' => 'markup',
-        '#prefix' => '<div class="'. $zebra .'"><div class="description">',
-        '#value' => t('@type: @class<p>Settings:<br />!settings</p>', array('@type' => ucfirst($data['type']), '@class' => $data['class'], '!settings' => $settings)),
+        '#prefix' => '<div class="' . $zebra . '"><div class="description">',
+        '#value' => t('@type: @class<p>Settings:<br />!settings</p>', array('@type' => drupal_ucfirst($data['type']), '@class' => $data['class'], '!settings' => $settings)),
         '#suffix' => '</div>',
       );
       $form[$data['step']][$key . $uniqid]['text'] = array(
         '#type' => 'markup',
-        '#title' => ucfirst($data['type']) .': '. $data['class'],
+        '#title' => drupal_ucfirst($data['type']) . ': ' . $data['class'],
         '#description' => t('Settings:<br />!settings', array('!settings' => $settings)),
-        '#value' => $data['value'] . '</div><strong>'. t('With visible HTML:') .'</strong><div class="flexifilter-plain-text">'. check_plain($data['value']),
-        '#prefix' => '<strong>'. t('As it appears to users:') .'</strong><div class="flexifilter-text">',
+        '#value' => $data['value'] . '</div><strong>' . t('With visible HTML:') . '</strong><div class="flexifilter-plain-text">' . check_plain($data['value']),
+        '#prefix' => '<strong>' . t('As it appears to users:') . '</strong><div class="flexifilter-text">',
         '#suffix' => '</div></div>',
       );
       $zebra = ($zebra == 'flexifilter-even') ? 'flexifilter-odd' : 'flexifilter-even';
@@ -944,4 +1028,4 @@ function flexifilter_filter_preview_form_submit($form, &$form_state) {
   $form_state['storage']['data'] = (variable_get('flexifilter_preview_text', array()));
   variable_del('flexifilter_preview');
   variable_del('flexifilter_preview_text');
-}
\ No newline at end of file
+}
diff --git flexifilter.components.inc flexifilter.components.inc
index 35a32bb..fa2affd 100644
--- flexifilter.components.inc
+++ flexifilter.components.inc
@@ -2,7 +2,12 @@
 // $Id: flexifilter.components.inc,v 1.14.2.2 2008/11/22 22:23:18 cwgordon7 Exp $
 
 /**
- * Implementation of hook_flexifilter_components()
+ * @file
+ * Contains the components to be used by the Flexifield module.
+ */
+
+/**
+ * Implements hook_flexifilter_components().
  *
  * @return An array of components to be used by the Flexifilter module. The keys of this array
  * are unique identifiers for the component (called the component class), and the values of the
@@ -129,12 +134,12 @@ function flexifilter_flexifilter_components() {
   );
 
   // Allow existing filters to be used as components
-  foreach (module_list() as $module) {
+  foreach (module_implements('filter') as $module) {
     if ($module !== 'flexifilter') {
       $list = module_invoke($module, 'filter', 'list');
       if (isset($list) && is_array($list)) {
         foreach ($list as $delta => $name) {
-          $components['flexifilter_existing__'. $module .'__filter__'. $delta] = array(
+          $components['flexifilter_existing__' . $module . '__filter__' . $delta] = array(
             'label' => $name,
             'description' => module_invoke($module, 'filter', 'description', $delta),
             'callback' => 'flexifilter_existing_filter_as_component',
@@ -149,7 +154,7 @@ function flexifilter_flexifilter_components() {
 
   // Allow existing flexifilters to be used as components
   foreach (flexifilter_get_filters(FALSE) as $fid => $filter) {
-    $components['flexifilter_existing_flexifilter__'. $fid] = array(
+    $components['flexifilter_existing_flexifilter__' . $fid] = array(
       'label' => $filter['label'],
       'description' => $filter['description'],
       'callback' => 'flexifilter_existing_flexifilter_as_component',
@@ -217,7 +222,7 @@ function flexifilter_component_text_regex_replace($op, $settings, $text = '') {
 
     case 'prepare':
     case 'process':
-      return preg_replace('~'. str_replace('~', '\~', $settings['find']) .'~', isset($settings['replace']) ? $settings['replace'] : '', $text);
+      return preg_replace('~' . str_replace('~', '\~', $settings['find']) . '~', isset($settings['replace']) ? $settings['replace'] : '', $text);
   }
   return $text;
 }
@@ -270,7 +275,7 @@ function flexifilter_component_while($op, $settings, $text) {
       // while (text contains "example") replace "example" with "e.g."
       // As the replacement would only run in processing, the while loop would loop
       // forever in preparation.
-      $limit = (integer)$settings['limit'];
+      $limit = (integer) $settings['limit'];
       $n = 0;
       if (flexifilter_components_involve_step($settings['components'], $op)) {
         while (flexifilter_invoke_condition($settings['condition'], $op, $text) == TRUE) {
@@ -293,7 +298,7 @@ function flexifilter_component_while($op, $settings, $text) {
  *
  * @param $settings
  *   The settings to set the global configuration to.
- * @see _flexifilter_pop_conf
+ * @see _flexifilter_pop_conf()
  */
 function _flexifilter_push_conf($settings) {
   global $conf;
@@ -310,7 +315,7 @@ function _flexifilter_push_conf($settings) {
  *
  * @param $old
  *   The old settings to revert back to.
- * @see _flexifilter_push_conf
+ * @see _flexifilter_push_conf()
  */
 function _flexifilter_pop_conf($old) {
   global $conf;
@@ -426,9 +431,10 @@ function flexifilter_component_text_pattern_replace($op, $settings, $text) {
 /**
  * Helper function for flexifilter_component_text_pattern_replace. Replaces text only once.
  * Cannot use str_replace with $count because Drupal supports PHP 4.
+ * GCI TODO: replace this with str_replace since D7 supports str_replace with $count.
  */
 function flexifilter_component_text_pattern_replace_once($pos, $search, $replace, $subject) {
-  return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
+  return drupal_substr($subject, 0, $pos) . $replace . drupal_substr($subject, $pos + drupal_strlen($search));
 }
 
 /**
@@ -488,21 +494,21 @@ function flexifilter_component_chunk_grab($op, $settings, $text) {
       while ($pos !== FALSE && $endpos !== FALSE) {
         $pos = $case_sensitive ? strpos($text, $start) : stripos($text, $start);
         if ($include_rest) {
-          $tmp = substr($text, 0, $pos);
+          $tmp = drupal_substr($text, 0, $pos);
         }
         if ($pos !== FALSE) {
           $endpos = $case_sensitive ? strpos($text, $end) : stripos($text, $end);
           if ($endpos !== FALSE) {
             if ($include_rest) {
-              $tmp2 = substr($text, $endpos + strlen($end));
+              $tmp2 = drupal_substr($text, $endpos + drupal_strlen($end));
             }
             if ($pass_limits) {
-              $endpos = $endpos + strlen($end);
+              $endpos = $endpos + drupal_strlen($end);
             }
             else {
-              $pos = $pos + strlen($start);
+              $pos = $pos + drupal_strlen($start);
             }
-            $chunk = substr($text, $pos, $endpos - $pos);
+            $chunk = drupal_substr($text, $pos, $endpos - $pos);
             $tmp .= flexifilter_invoke_components($settings['components'], $op, $chunk);
             $tmp .= $tmp2;
             $text = $tmp;
@@ -514,15 +520,6 @@ function flexifilter_component_chunk_grab($op, $settings, $text) {
 }
 
 /**
- * Replacement for stripos() for PHP 4.x.
- */
-if (!function_exists('stripos')) {
-  function stripos($haystack, $needle) {
-    return strpos(strtolower($haystack), strtolower($needle));
-  }
-}
-
-/**
  * Flexifilter component callback.
  * Simple text append.
  */
@@ -595,9 +592,9 @@ function flexifilter_component_text_substr($op, $settings, $text) {
       $substr_start = isset($settings['substr_start']) ? $settings['substr_start'] : '0';
       $substr_length = isset($settings['substr_length']) ? $settings['substr_length'] : '';
       if ($substr_length === '') {
-        return substr($text, $substr_start);
+        return drupal_substr($text, $substr_start);
       }
-      return substr($text, $substr_start, $substr_length);
+      return drupal_substr($text, $substr_start, $substr_length);
   }
 }
 
@@ -692,17 +689,16 @@ function flexifilter_component_sequences($op, $settings, $text) {
   }
 }
 
-
 function flexifilter_sequences_replace_once($search, $replace, &$subject) {
   if (($pos = strpos($subject, $search)) !== FALSE) {
-    $subject = substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
+    $subject = drupal_substr($subject, 0, $pos) . $replace . drupal_substr($subject, $pos + drupal_strlen($search));
     return TRUE;
   }
   return FALSE;
 }
 
 /**
- * Implementation of hook_flexifilter_sequences.
+ * Implements hook_flexifilter_sequences().
  */
 function flexifilter_flexifilter_sequences() {
   return array(
@@ -720,10 +716,10 @@ function flexifilter_sequence_numbers($num) {
 }
 
 function flexifilter_sequence_letters_uc($num) {
-  return strtoupper(flexifilter_sequence_letters_lc($num));
+  return drupal_strtoupper(flexifilter_sequence_letters_lc($num));
 }
 
 function flexifilter_sequence_letters_lc($num) {
   $letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
   return $letters[$num % 26];
-}
\ No newline at end of file
+}
diff --git flexifilter.conditions.inc flexifilter.conditions.inc
index b4f171c..c7f4ae6 100644
--- flexifilter.conditions.inc
+++ flexifilter.conditions.inc
@@ -2,7 +2,12 @@
 // $Id: flexifilter.conditions.inc,v 1.6 2008/01/22 00:48:28 cwgordon7 Exp $
 
 /**
- * Implementation of hook_flexifilter_conditions()
+ * @file
+ * Contains the condition callback functions for the Flexifield module.
+ */
+
+/**
+ * Implements hook_flexifilter_conditions().
  */
 function flexifilter_flexifilter_conditions() {
   $conditions = array();
@@ -104,7 +109,7 @@ function flexifilter_condition_text_search($op, $settings, $text) {
     case 'prepare':
     case 'process':
       if (isset($settings['is_regex']) && $settings['is_regex'] == 1) {
-        return preg_match('~'. str_replace('~', '\~', $settings['find']) .'~', $text) == 1;
+        return preg_match('~' . str_replace('~', '\~', $settings['find']) . '~', $text) == 1;
       }
       else {
         return strpos($text, $settings['find']) !== FALSE;
@@ -144,25 +149,25 @@ function flexifilter_condition_text_length($op, $settings, $text) {
 
     case 'prepare':
     case 'process':
-      $length   = strlen($text);
+      $length   = drupal_strlen($text);
       $operator = isset($settings['operator']) ? $settings['operator'] : 0;
-      $num      = isset($settings['num']) ? $settings['num'] : variable_get('teaser_length', 600);;
+      $num      = isset($settings['num']) ? $settings['num'] : variable_get('teaser_length', 600);
       switch ($operator) {
-      case 0:
-        return ($length < $num);
+        case 0:
+          return ($length < $num);
 
-      case 1:
-        return ($length <= $num);
+        case 1:
+          return ($length <= $num);
 
-      case 2:
-        return ($length == $num);
+        case 2:
+          return ($length == $num);
 
-      case 3:
-        return ($length >= $num);
+        case 3:
+          return ($length >= $num);
 
-      case 4:
-        return ($length > $num);
-    }
+        case 4:
+          return ($length > $num);
+      }
   }
 }
 
diff --git flexifilter.flexifilters.inc flexifilter.flexifilters.inc
index 635cb48..12caa52 100644
--- flexifilter.flexifilters.inc
+++ flexifilter.flexifilters.inc
@@ -2,7 +2,12 @@
 // $Id: flexifilter.flexifilters.inc,v 1.4 2008/01/22 00:48:28 cwgordon7 Exp $
 
 /**
- * Implementation of hook_flexifilters.
+ * @file
+ * Contains sample filters for the Flexifield module.
+ */
+
+/**
+ * Implements hook_flexifilters().
  */
 function flexifilter_flexifilters() {
   return array(
@@ -10,8 +15,8 @@ function flexifilter_flexifilters() {
       'label' => 'Mediawiki Format',
       'description' => 'This is an example format which provides a wikimedia-style input format.',
       'id' => '1',
-      'enabled' => true,
-      'advanced' => true,
+      'enabled' => TRUE,
+      'advanced' => TRUE,
       'delta' => '0',
       'components' => array(
         array(
@@ -180,7 +185,7 @@ function flexifilter_flexifilters() {
                     ),
                     array(
                       'class' => 'flexifilter_control_if',
-                      'settings' =>  array(
+                      'settings' => array(
                         'components' => array(
                           array(
                             'class' => 'flexifilter_text_prepend',
@@ -232,4 +237,4 @@ function flexifilter_flexifilters() {
       'fid' => 'new',
     ),
   );
-}
\ No newline at end of file
+}
diff --git flexifilter.info flexifilter.info
index b5189b6..914f026 100644
--- flexifilter.info
+++ flexifilter.info
@@ -2,10 +2,10 @@
 name = Flexifilter
 description = Allows creatation of flexible input filters entirely through the user interface.
 package = Flexifilter
-core = 6.x
-; Information added by drupal.org packaging script on 2010-05-01
-version = "6.x-1.2"
-core = "6.x"
-project = "flexifilter"
-datestamp = "1272687006"
-
+core = 7.x
+files[] = flexifilter.admin.inc
+files[] = flexifilter.components.inc
+files[] = flexifilter.conditions.inc
+files[] = flexifilter.flexifilters.inc
+files[] = flexifilter.install
+files[] = flexifilter.module
diff --git flexifilter.install flexifilter.install
index 7398a0d..e44c3a2 100644
--- flexifilter.install
+++ flexifilter.install
@@ -2,32 +2,34 @@
 // $Id: flexifilter.install,v 1.6.2.4 2008/11/22 22:13:33 cwgordon7 Exp $
 
 /**
- * Implementation of hook_install().
+ * @file
+ * Install, update and uninstall functions for the Flexifield module.
+ */
+
+/**
+ * Implements hook_install().
  */
 function flexifilter_install() {
-  drupal_install_schema('flexifilter');
-  require_once drupal_get_path('module', 'flexifilter') .'/flexifilter.module';
   flexifilter_install_flexifilters('flexifilter');
 }
 
 /**
- * Implementation of hook_uninstall().
+ * Implements hook_uninstall().
  */
 function flexifilter_uninstall() {
-  drupal_uninstall_schema('flexifilter');
 }
 
 /**
- * Implementation of hook_schema().
+ * Implements hook_schema().
  */
 function flexifilter_schema() {
   $schema = array();
 
   $schema['flexifilters'] = array(
-    'description' => t('Holds definitions for flexifilters.'),
+    'description' => 'Holds definitions for flexifilters.',
     'fields' => array(
       'fid' => array(
-        'description' => t('The primary identifier for a flexifilter.'),
+        'description' => 'The primary identifier for a Flexifilter.',
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
@@ -36,13 +38,13 @@ function flexifilter_schema() {
         'type' => 'varchar',
         'length' => 127,
         'not null' => TRUE,
-        'description' => t('Flexifilter human-readable name.'),
+        'description' => 'Flexifilter human-readable name.',
       ),
       'description' => array(
         'type' => 'varchar',
         'length' => 255,
         'not null' => TRUE,
-        'description' => t('Flexifilter filter tips.'),
+        'description' => 'Flexifilter filter tips.',
       ),
       'enabled' => array(
         'type' => 'int',
@@ -50,22 +52,15 @@ function flexifilter_schema() {
         'size' => 'tiny',
         'not null' => TRUE,
         'default' => 0,
-        'description' => t('1 if the flexifilter is enabled, 0 otherwise'),
+        'description' => '1 if the Flexifilter is enabled, 0 otherwise',
         'disp-width' => '3',
       ),
-      'delta' => array(
-        'type' => 'int',
-        'size' => 'tiny',
-        'not null' => TRUE,
-        'disp-width' => '4',
-        'description' => t('Used to map Flexifilters to filters.'),
-      ),
       'pid_root' => array(
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => FALSE,
         'disp-width' => '10',
-        'description' => t('ID of a faux flexifilter part used as a parent for the root level components'),
+        'description' => 'ID of a faux Flexifilter part used as a parent for the root level components',
       ),
       'advanced' => array(
         'type' => 'int',
@@ -73,7 +68,7 @@ function flexifilter_schema() {
         'size' => 'tiny',
         'not null' => TRUE,
         'disp-width' => '3',
-        'description' => t('1 if the flexifilter admin interface should display advanced settings for this flexifilter, 0 otherwise'),
+        'description' => '1 if the Flexifilter admin interface should display advanced settings for this Flexifilter, 0 otherwise',
       ),
       'cache' => array(
         'type' => 'int',
@@ -81,38 +76,35 @@ function flexifilter_schema() {
         'size' => 'tiny',
         'not null' => TRUE,
         'default' => 0,
-        'description' => t('1 if the flexifilter has caching enabled, 0 otherwise. Used for dynamic filters.'),
+        'description' => '1 if the Flexifilter has caching enabled, 0 otherwise. Used for dynamic filters.',
       ),
     ),
     'primary key' => array('fid'),
-    'indexes' => array(
-      'delta' => array('delta'),
-    ),
   );
 
   $schema['flexifilters_parts'] = array(
-    'description' => t('Holds definitions for individual flexifilter parts (components and conditions).'),
+    'description' => 'Holds definitions for individual Flexifilter parts (components and conditions).',
     'fields' => array(
       'pid' => array(
         'type' => 'serial',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'disp-width' => '10',
-        'description' => t('Primary identifier for a flexifilter part.'),
+        'description' => 'Primary identifier for a Flexifilter part.',
       ),
       'fid' => array(
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'disp-width' => '10',
-        'description' => t('ID of the flexifilter containing this part.'),
+        'description' => 'ID of the Flexifilter containing this part.',
       ),
       'parent_pid' => array(
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => FALSE,
         'disp-width' => '10',
-        'description' => t('ID of the flexifilter part that directly contains this part.'),
+        'description' => 'ID of the Flexifilter part that directly contains this part.',
       ),
       'type' => array(
         'type' => 'int',
@@ -120,19 +112,19 @@ function flexifilter_schema() {
         'size' => 'tiny',
         'not null' => TRUE,
         'disp-width' => '3',
-        'description' => t('Type number of this part (component / condition / root)'),
+        'description' => 'Type number of this part (component / condition / root)',
       ),
       'class_name' => array(
         'type' => 'varchar',
         'length' => '255',
         'not null' => FALSE,
-        'description' => t('Class name of the component / condition'),
+        'description' => 'Class name of the component / condition',
       ),
       'settings' => array(
         'type' => 'text',
         'size' => 'big',
         'not null' => TRUE,
-        'description' => t('Serialized array of any settings that have been set for this part'),
+        'description' => 'Serialized array of any settings that have been set for this part',
       ),
     ),
     'primary key' => array('pid'),
@@ -143,22 +135,3 @@ function flexifilter_schema() {
 
   return $schema;
 }
-
-/**
- * Update function: add the 'cache' field to the {flexifilters} table.
- */
-function flexifilter_update_6100($ret) {
-  $table = 'flexifilters';
-  $field_name = 'cache';
-  $field_def = array(
-    'type' => 'int',
-    'unsigned' => TRUE,
-    'size' => 'tiny',
-    'not null' => TRUE,
-    'default' => 0,
-    'description' => t('1 if the flexifilter has caching enabled, 0 otherwise. Used for dynamic filters.'),
-    'initial' => 1,
-  );
-  db_add_field($ret, $table, $field_name, $field_def);
-  return $ret;
-}
\ No newline at end of file
diff --git flexifilter.module flexifilter.module
index 1bb85b8..0daff97 100644
--- flexifilter.module
+++ flexifilter.module
@@ -2,29 +2,32 @@
 // $Id: flexifilter.module,v 1.16.2.7 2008/11/22 22:13:33 cwgordon7 Exp $
 
 $path = drupal_get_path('module', 'flexifilter');
-include_once ($path .'/flexifilter.components.inc');
-include_once ($path .'/flexifilter.conditions.inc');
-include_once ($path .'/flexifilter.flexifilters.inc');
+include_once DRUPAL_ROOT . '/' . ($path . '/flexifilter.components.inc');
+include_once DRUPAL_ROOT . '/' . ($path . '/flexifilter.conditions.inc');
+include_once DRUPAL_ROOT . '/' . ($path . '/flexifilter.flexifilters.inc');
 
 define('FLEXIFILTER_PART_TYPE_COMPONENT', 0);
 define('FLEXIFILTER_PART_TYPE_CONDITION', 1);
 define('FLEXIFILTER_PART_TYPE_ROOT', 2);
 
-define('FLEXIFILTER_MAX_FILTERS', 128);
-
 /**
- * Implementation of hook_perm().
+ * Implements hook_permission().
  */
-function flexifilter_perm() {
-  return array('administer flexifilter');
+function flexifilter_permission() {
+  return array(
+    'administer flexifilter' => array(
+      'title' => t('Administer flexifilter'),
+      'description' => t('Change administrative settings for flexifilter'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_menu().
+ * Implements hook_menu().
  */
 function flexifilter_menu() {
   $items = array();
-  $items['admin/build/flexifilters'] = array(
+  $items['admin/structure/flexifilters'] = array(
     'title' => 'Flexifilters',
     'description' => 'Create new flexible input filters without writing any code.',
     'page callback' => 'drupal_get_form',
@@ -32,11 +35,11 @@ function flexifilter_menu() {
     'page arguments' => array('flexifilter_filter_list_form'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/list'] = array(
+  $items['admin/structure/flexifilters/list'] = array(
     'title' => 'List',
     'type' => MENU_DEFAULT_LOCAL_TASK,
   );
-  $items['admin/build/flexifilters/add'] = array(
+  $items['admin/structure/flexifilters/add'] = array(
     'title' => 'Add new flexifilter',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('flexifilter_filter_edit_form'),
@@ -45,7 +48,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/import'] = array(
+  $items['admin/structure/flexifilters/import'] = array(
     'title' => 'Import a flexifilter',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('flexifilter_filter_input_form'),
@@ -54,7 +57,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/defaults'] = array(
+  $items['admin/structure/flexifilters/defaults'] = array(
     'title' => 'Load a default flexifilter',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('flexifilter_filter_default_form'),
@@ -63,7 +66,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter'] = array(
+  $items['admin/structure/flexifilters/%flexifilter'] = array(
     'title callback' => 'flexifilter_get_field',
     'title arguments' => array(3, 'label'),
     'type' => MENU_CALLBACK,
@@ -72,12 +75,12 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter/edit'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/edit'] = array(
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'access arguments' => array('administer flexifilter'),
     'title' => 'Edit',
   );
-  $items['admin/build/flexifilters/%flexifilter/export'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/export'] = array(
     'title' => 'Export',
     'type' => MENU_LOCAL_TASK,
     'page callback' => 'drupal_get_form',
@@ -85,7 +88,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter/preview'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/preview'] = array(
     'title' => 'Preview',
     'type' => MENU_LOCAL_TASK,
     'page callback' => 'drupal_get_form',
@@ -93,7 +96,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter/delete'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/delete'] = array(
     'title' => 'Delete',
     'type' => MENU_CALLBACK,
     'page callback' => 'drupal_get_form',
@@ -101,7 +104,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter/enable'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/enable'] = array(
     'title' => 'Enable',
     'type' => MENU_CALLBACK,
     'page callback' => 'drupal_get_form',
@@ -109,7 +112,7 @@ function flexifilter_menu() {
     'access arguments' => array('administer flexifilter'),
     'file' => 'flexifilter.admin.inc',
   );
-  $items['admin/build/flexifilters/%flexifilter/disable'] = array(
+  $items['admin/structure/flexifilters/%flexifilter/disable'] = array(
     'title' => 'Disable',
     'type' => MENU_CALLBACK,
     'page callback' => 'drupal_get_form',
@@ -121,21 +124,21 @@ function flexifilter_menu() {
 }
 
 /**
- * Implementation of hook_help()
+ * Implements hook_help().
  */
 function flexifilter_help($path, $arg) {
   switch ($path) {
     case 'admin/help#flexifilter':
-      $output = '<p>'. t('Flexifilters are flexible filters that can be used by <a href="@input_formats">Input Formats</a> to filter created content.', array('@input_formats' => url('admin/settings/filters'))) .'</p>';
-      $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@flexifilter">Flexifilter module</a>.', array('@flexifilter' => 'http://drupal.org/node/212410/')) .'</p>';
+      $output = '<p>' . t('Flexifilters are flexible filters that can be used by <a href="@input_formats">Input Formats</a> to filter created content.', array('@input_formats' => url('admin/config/filter'))) . '</p>';
+      $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@flexifilter">Flexifilter module</a>.', array('@flexifilter' => 'http://drupal.org/node/212410/')) . '</p>';
       return $output;
 
-    case 'admin/build/flexifilters':
-      return '<p>'. t('Flexifilters are flexible filters that can be used by <a href="@input_formats">Input Formats</a> to filter created content.', array('@input_formats' => url('admin/settings/filters'))) .'</p>';
+    case 'admin/structure/flexifilters':
+      return '<p>' . t('Flexifilters are flexible filters that can be used by <a href="@input_formats">Input Formats</a> to filter created content.', array('@input_formats' => url('admin/config/filter'))) . '</p>';
 
-    case 'admin/build/flexifilters/%/edit':
-      $output = '<p>'. t('Flexifilters are defined in terms of components. A component can range from something simple like "regex replacement" to "invoke other filter" or "while loop". By combining these components, you can create a filter to do what you want. Use the "Add component" dropdown to add a new component to the end of your filter, and then use the "Re/move" dropdown within the component to remove the component, or to move it up/down within the list of components.') .'</p>';
-      $output .= '<p>'. t('Some components (for example, the if/while loops) can have components and conditions within them. In this case, the if/while has a list of components, just like the filter itself has a list of components. The if/while will also have a condition, which controls when the list of components happen.');
+    case 'admin/structure/flexifilters/%/edit':
+      $output = '<p>' . t('Flexifilters are defined in terms of components. A component can range from something simple like "regex replacement" to "invoke other filter" or "while loop". By combining these components, you can create a filter to do what you want. Use the "Add component" dropdown to add a new component to the end of your filter, and then use the "Re/move" dropdown within the component to remove the component, or to move it up/down within the list of components.') . '</p>';
+      $output .= '<p>' . t('Some components (for example, the if/while loops) can have components and conditions within them. In this case, the if/while has a list of components, just like the filter itself has a list of components. The if/while will also have a condition, which controls when the list of components happen.');
       return $output;
   }
 }
@@ -244,7 +247,13 @@ function flexifilter_invoke_component($component, $op, $settings = array(), $tex
   $_flexifilter_preview = variable_get('flexifilter_preview', FALSE);
   if ($_flexifilter_preview) {
     $_flexifilter_preview_text = variable_get('flexifilter_preview_text', array());
-    $_flexifilter_preview_text[] = array('value' => $text, 'type' => 'component', 'step' => $op, 'class' => $component['label'], 'settings' => $settings);
+    $_flexifilter_preview_text[] = array(
+      'value' => $text,
+      'type' => 'component',
+      'step' => $op,
+      'class' => $component['label'],
+      'settings' => $settings,
+    );
     variable_set('flexifilter_preview_text', $_flexifilter_preview_text);
   }
   return $text;
@@ -280,7 +289,6 @@ function flexifilter_invoke_condition($data, $op, $text = '') {
 
   switch ($op) {
     // For settings, return the value as-is
-
     case 'settings':
       return $return_value;
 
@@ -370,58 +378,37 @@ function flexifilter_get_grouped_labels($elements) {
   return $grouped;
 }
 
-/**
- * Implementation of hook_filter
- *
- * Maps flexifilters into the existing filters system. Currently maps $delta to the
- * flexifilter ID. This will have to change, as the filter system stores delta as a
- * tinyint, and flexifilter IDs might exceed 127. Possible solutions:
- *  - Limit the number of enabled flexifilters to 128 at any one time (and unlimited disabled ones)
- *  - Expose a single "Flexifilter" filter, and allow it to be configured, setting which flexifilters to use
- */
-function flexifilter_filter($op, $delta = 0, $format = -1, $text = '') {
-  switch ($op) {
-    case 'list':
-      $filters = array();
-      foreach (flexifilter_get_filters(FALSE) as $fid => $filter) {
-        if ($filter['enabled']) {
-          $filters[$filter['delta']] = $filter['label'];
-        }
-      }
-      return $filters;
-
-    case 'description':
-      $filter = flexifilter_get_filter_by_delta($delta);
-      return $filter['description'];
-
-    case 'prepare':
-    case 'process':
-      $filter = flexifilter_get_filter_by_delta($delta);
-      return flexifilter_invoke_components($filter['components'], $op, $text);
+function flexifilter_filter_info() {
+  $filters = array();
+  foreach (flexifilter_get_filters(FALSE) as $fid => $filter) {
+    $filters[$filter['label']] = array(
+      'title' => t($filter['label']),
+      'description' => t($filter['description']),
+      'prepare callback' => 'flexifilter_filter_prepare',
+      'process callback' => 'flexifilter_filter_process',
+      'tips callback' => 'flexifilter_filter_tips',
+    );
+  }
+}
 
-    case 'no cache':
-      $filter = flexifilter_get_filter_by_delta($delta);
-      return !($filter['cache']);
+function flexifilter_filter_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
+  return flexifilter_invoke_components($filter['components'], 'prepare', $text);
+}
 
-    default:
-      return $text;
-  }
+function flexifilter_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
+  return flexifilter_invoke_components($filter['components'], 'process', $text);
 }
 
-/**
- * Implementation of hook_filter_tips.
- */
-function flexifilter_filter_tips($delta, $format, $long = FALSE) {
-  $flexifilter = flexifilter_get_filter_by_delta($delta);
+function flexifilter_filter_tips($filter, $format, $long = FALSE) {
   if ($long) {
     return str_replace('<--break-->', '', $flexifilter['description']);
   }
   else {
     $pos = strpos($flexifilter['description'], '<!--break-->');
     if ($pos === FALSE) {
-      return substr($flexifilter['description'], 0);
+      return drupal_substr($flexifilter['description'], 0);
     }
-	  return substr($flexifilter['description'], 0, $pos);
+    return drupal_substr($flexifilter['description'], 0, $pos);
   }
 }
 
@@ -437,8 +424,9 @@ function flexifilter_load($fid) {
   if (!is_numeric($fid)) {
     return FALSE;
   }
-  $result = db_query('SELECT * FROM {flexifilters} WHERE fid = %d', $fid);
-  if ($row = db_fetch_object($result)) {
+  $result = db_query('SELECT * FROM {flexifilters} WHERE fid = :fid', array(':fid' => $fid));
+
+  foreach ($result as $row) {
     return _flexifilter_filter_from_db_row($row, TRUE);
   }
   return FALSE;
@@ -461,7 +449,6 @@ function flexifilter_get_field($flexifilter, $field) {
  *  - id : The flexifilter ID number
  *  - enabled : TRUE if the flexifilter is enabled, FALSE otherwise
  *  - advanced
- *  - delta : If enabled, then a value between 0 and 127 which is unique in the enabled flexifilters
  *  - components : An array containing the components of the flexifilter
  */
 function flexifilter_get_filters($include_components = TRUE, $reset = FALSE) {
@@ -469,7 +456,7 @@ function flexifilter_get_filters($include_components = TRUE, $reset = FALSE) {
   if (!isset($cache[$include_components]) || $reset) {
     $filters = array();
     $result = db_query('SELECT * FROM {flexifilters}');
-    while ($row = db_fetch_object($result)) {
+    foreach ($result as $row) {
       $filters[$row->fid] = _flexifilter_filter_from_db_row($row, $include_components);
     }
     $cache[$include_components] = $filters;
@@ -481,9 +468,8 @@ function flexifilter_get_filters($include_components = TRUE, $reset = FALSE) {
  * Saves a flexifilter to the database.
  *
  * @param $filter A flexifilter to save. Should be of same form as the flexifilters returned from flexifilter_get_filters
- * (e.g. an array containing the label,description,id,enabled,advanced,delta and components fields), although id can be
- * set to 'new' to create a new flexifilter rather than update an existing one (in which case, delta is ignored). Do
- * not use
+ * (e.g. an array containing the label,description,id,enabled,advanced and components fields), although id can be
+ * set to 'new' to create a new flexifilter rather than update an existing one. Do not use
  *
  * @return The fid.
  */
@@ -494,40 +480,57 @@ function flexifilter_save_filter($filter) {
   $advanced    = $filter['advanced'];
   $cache       = isset($filter['cache']) ? $filter['cache'] : 1;
   if ($fid === 'new') {
-    db_query("INSERT INTO {flexifilters} (label, description, enabled, delta, pid_root, advanced, cache) VALUES ('%s', '%s', 0, 0, 0, %d, %d)",
-      $label, $description, $advanced, $cache);
-    $fid           = db_last_insert_id('flexifilters', 'fid');
+    $fid = db_insert('flexifilters')
+  ->fields(array(
+      'label' => $label,
+      'description' => $description,
+      'enabled' => 0,
+      'pid_root' => 0,
+      'advanced' => $advanced,
+      'cache' => $cache,
+    ))
+  ->execute();
     $pids_to_reuse = array();
     $pid_root      = _flexifilter_save_filter_components($filter['components'], $fid, $pids_to_reuse);
-    db_query('UPDATE {flexifilters} SET pid_root = %d WHERE fid = %d', $pid_root, $fid);
+    db_update('flexifilters')
+  ->fields(array(
+      'pid_root' => $pid_root,
+    ))
+  ->condition('fid', $fid)
+  ->execute();
   }
   else {
     $pids_to_reuse = array();
-    $result = db_query('SELECT pid FROM {flexifilters_parts} WHERE fid = %d', $fid);
-    while ($row = db_fetch_object($result)) {
+    $result = db_query('SELECT pid FROM {flexifilters_parts} WHERE fid = :fid', array(':fid' => $fid));
+    foreach ($result as $row) {
       $pids_to_reuse[] = $row->pid;
     }
     sort($pids_to_reuse, SORT_NUMERIC);
     $pid_root = _flexifilter_save_filter_components($filter['components'], $fid, $pids_to_reuse);
     if (count($pids_to_reuse) > 0) {
-      db_query('DELETE FROM {flexifilters_parts} WHERE pid = '. implode($pids_to_reuse, ' OR pid = '), $fid);
+      db_query('DELETE FROM {flexifilters_parts} WHERE pid = ' . implode($pids_to_reuse, ' OR pid = '), $fid);
     }
-    db_query("UPDATE {flexifilters} SET label = '%s', description = '%s', pid_root = %d, advanced = %d, cache = %d WHERE fid = %d",
-      $label, $description, $pid_root, $advanced, $cache, $fid);
+    db_update('flexifilters')
+  ->fields(array(
+      'label' => $label,
+      'description' => $description,
+      'pid_root' => $pid_root,
+      'advanced' => $advanced,
+      'cache' => $cache,
+    ))
+  ->condition('fid', $fid)
+  ->execute();
   }
   $existing_filters = flexifilter_get_filters(FALSE, TRUE);
   if (isset($existing_filters[$fid]['enabled']) && isset($filter['enabled'])) {
     if ($existing_filters[$fid]['enabled'] != $filter['enabled']) {
-      if ($filter['enabled']) {
-        if (flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS) {
-          $delta = flexifilter_get_unused_delta();
-          if ($delta !== FALSE) {
-            db_query('UPDATE {flexifilters} SET enabled = 1, delta = %d WHERE fid = %d', $delta, $fid);
-          }
-        }
-      }
-      else {
-        db_query('UPDATE {flexifilters} SET enabled = 0 WHERE fid = %d', $fid);
+      if (!$filter['enabled']) {
+        db_update('flexifilters')
+  ->fields(array(
+          'enabled' => 0,
+        ))
+  ->condition('fid', $fid)
+  ->execute();
       }
     }
   }
@@ -537,14 +540,28 @@ function flexifilter_save_filter($filter) {
 function _flexifilter_save_filter_components($components, $fid, &$pids_to_reuse, $parent = FALSE) {
   if ($parent === FALSE) {
     if ($reuse_pid = array_shift($pids_to_reuse)) {
-      db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d",
-        $fid, FLEXIFILTER_PART_TYPE_ROOT, $reuse_pid);
+      db_update('flexifilters_parts')
+  ->fields(array(
+        'fid' => $fid,
+        'parent_pid' => 0,
+        'type' => FLEXIFILTER_PART_TYPE_ROOT,
+        'class_name' => '',
+        'settings' => '',
+      ))
+  ->condition('pid', $reuse_pid)
+  ->execute();
       $parent = $reuse_pid;
     }
     else {
-      db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, 0, %d, '', '')",
-        $fid, FLEXIFILTER_PART_TYPE_ROOT);
-      $parent = db_last_insert_id('flexifilters_parts', 'pid');
+     $parent = db_insert('flexifilters_parts')
+  ->fields(array(
+        'fid' => $fid,
+        'parent_pid' => 0,
+        'type' => FLEXIFILTER_PART_TYPE_ROOT,
+        'class_name' => '',
+        'settings' => '',
+      ))
+  ->execute();
     }
   }
   foreach ($components as $key => $child) {
@@ -558,13 +575,27 @@ function _flexifilter_save_filter_components($components, $fid, &$pids_to_reuse,
         unset($child['settings']['condition']);
       }
       if ($child_cid = array_shift($pids_to_reuse)) {
-        db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child_cid);
+        db_update('flexifilters_parts')
+  ->fields(array(
+          'fid' => $fid,
+          'parent_pid' => $parent,
+          'type' => FLEXIFILTER_PART_TYPE_COMPONENT,
+          'class_name' => $child['class'],
+          'settings' => serialize($child['settings']),
+        ))
+  ->condition('pid', $child_cid)
+  ->execute();
       }
       else {
-        db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']));
-        $child_cid = db_last_insert_id('flexifilters_parts', 'pid');
+        $child_cid = db_insert('flexifilters_parts')
+  ->fields(array(
+          'fid' => $fid,
+          'parent_pid' => $parent,
+          'type' => FLEXIFILTER_PART_TYPE_COMPONENT,
+          'class_name' => $child['class'],
+          'settings' => serialize($child['settings']),
+        ))
+  ->execute();
       }
       if (isset($child_components)) {
         _flexifilter_save_filter_components($child_components, $fid, $pids_to_reuse, $child_cid);
@@ -583,13 +614,27 @@ function _flexifilter_save_filter_condition($condition, $fid, &$pids_to_reuse, $
     unset($condition['settings']['conditions']);
   }
   if ($our_pid = array_shift($pids_to_reuse)) {
-    db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']), $our_pid);
+    db_update('flexifilters_parts')
+  ->fields(array(
+      'fid' => $fid,
+      'parent_pid' => $parent,
+      'type' => FLEXIFILTER_PART_TYPE_CONDITION,
+      'class_name' => $condition['class'],
+      'settings' => serialize($condition['settings']),
+    ))
+  ->condition('pid', $our_pid)
+  ->execute();
   }
   else {
-    db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']));
-    $our_pid = db_last_insert_id('flexifilters_parts', 'pid');
+    $our_pid = db_insert('flexifilters_parts')
+  ->fields(array(
+      'fid' => $fid,
+      'parent_pid' => $parent,
+      'type' => FLEXIFILTER_PART_TYPE_CONDITION,
+      'class_name' => $condition['class'],
+      'settings' => serialize($condition['settings']),
+    ))
+  ->execute();
   }
   if (isset($condition_conditions)) {
     foreach ($condition_conditions as $sub_condition) {
@@ -598,24 +643,21 @@ function _flexifilter_save_filter_condition($condition, $fid, &$pids_to_reuse, $
   }
 }
 
-/**
- * Fetches flexifilters by delta.
- */
-function flexifilter_get_filter_by_delta($delta, $reset = FALSE) {
+function flexifilter_get_filter_by_label($label, $reset = FALSE) {
   static $cache;
   if (!isset($cache)) {
     $cache = array();
   }
-  if (!isset($cache[$delta]) || $reset) {
-    $result = db_query('SELECT * FROM {flexifilters} WHERE enabled = 1 AND delta = %d', $delta);
-    if ($row = db_fetch_object($result)) {
-      $cache[$delta] = _flexifilter_filter_from_db_row($row, TRUE);
+  if (!isset($cache[$label]) || $reset) {
+    $result = db_query('SELECT * FROM {flexifilters} WHERE enabled = 1 AND label = :label', array(':label' => $label));
+    if ($result) {
+      $cache[$label] = _flexifilter_filter_from_db_row($result, TRUE);
     }
     else {
-      $cache[$delta] = FALSE;
+      $cache[$label] = FALSE;
     }
   }
-  return $cache[$delta];
+  return $cache[$label];
 }
 
 /**
@@ -629,18 +671,17 @@ function _flexifilter_filter_from_db_row($row, $include_components) {
     'id' => $row->fid,
     'enabled' => ($row->enabled == 1),
     'advanced' => ($row->advanced == 1),
-    'delta' => $row->delta,
     'cache' => $row->cache,
   );
   $pid_root = $row->pid_root;
 
   if ($include_components) {
     // Fetch all the parts of the filter and store them in a flat array
-    $result             = db_query('SELECT * FROM {flexifilters_parts} WHERE fid = %d', $row->fid);
+    $result             = db_query('SELECT * FROM {flexifilters_parts} WHERE fid = :fid', array(':fid' => $row->fid));
     $components_flat    = array();
     $component_children = array();
     $id_next            = 0;
-    while ($row = db_fetch_object($result)) {
+    foreach ($result as $row) {
       $components_flat[$row->pid] = array(
         'class' => $row->class_name,
         'settings' => $row->settings,
@@ -755,36 +796,19 @@ function flexifilter_get_number_enabled_filters($reset = FALSE) {
 function flexifilter_install_flexifilters($module, $flexifilters = NULL) {
   if (is_null($flexifilters)) {
     // Allow for magic file naming.
-    $file = drupal_get_path('module', $module) ."/$module.flexifilters.inc";
+    $file = drupal_get_path('module', $module) . '/$module.flexifilters.inc';
     if (file_exists($file)) {
-      require_once $file;
+      require_once DRUPAL_ROOT . '/' . $file;
     }
     $flexifilters = module_invoke($module, 'flexifilters');
   }
   $fids = array();
   foreach ($flexifilters as $flexifilter) {
     $fid = flexifilter_save_filter($flexifilter);
-    drupal_set_message(t('The !url flexifilter has been saved.', array('!url' => l($flexifilter['label'], "admin/build/flexifilters/$fid"))));
-    watchdog('flexifilter', 'The !url flexifilter has been saved.', array('!url' => l($flexifilter['label'], "admin/build/flexifilters/$fid")));
+    drupal_set_message(t('The !url flexifilter has been saved.', array('!url' => l($flexifilter['label'], "admin/structure/flexifilters/$fid"))));
+    watchdog('flexifilter', 'The !url flexifilter has been saved.', array('!url' => l($flexifilter['label'], "admin/structure/flexifilters/$fid")));
     $fids[] = $fid;
   }
   return $fids;
 }
 
-function flexifilter_get_unused_delta() {
-  $deltas = array();
-  for ($i = 0; $i < FLEXIFILTER_MAX_FILTERS; $i++) {
-    $deltas[$i] = TRUE;
-  }
-  foreach (flexifilter_get_filters(FALSE) as $flexifilter) {
-    if ($flexifilter['enabled']) {
-      $deltas[$flexifilter['delta']] = FALSE;
-    }
-  }
-  for ($i = 0; $i < FLEXIFILTER_MAX_FILTERS; $i++) {
-    if ($deltas[$i] == TRUE) {
-      return $i;
-    }
-  }
-  return FALSE;
-}
\ No newline at end of file
