diff --git taxonomy_access.module taxonomy_access.module
index 5bc1617..650acb0 100644
--- taxonomy_access.module
+++ taxonomy_access.module
@@ -253,7 +253,8 @@ function taxonomy_access_init() {
 function taxonomy_access_theme() {
   return array(
     'taxonomy_access_admin_form' => array(
-      'arguments' => array('form' => NULL),
+      'render element' => 'form',
+      'file' => 'taxonomy_access_admin.inc',
     ),
   );
 }
@@ -289,7 +290,7 @@ function taxonomy_access_menu() {
 
 
 /**
- * Implements hook_form_alter().
+ * Implements hook_form_BASE_FORM_ID_alter().
  *
  * @todo
  *     Move control of "create" op here
@@ -297,11 +298,11 @@ function taxonomy_access_menu() {
  *     Look at feasability to eliminate _restore_terms and _preserve_terms
  *     by simply setting the '#access' attribute for those terms.
  */
-function taxonomy_access_form_alter(&$form, &$form_state, $form_id) {
-  if ($form['#id'] == 'node-form' && is_numeric($form['nid']['#value'])) {
+function taxonomy_access_form_node_form_alter(&$form, &$form_state, $form_id) {
+  if (!empty($form_state['node']->nid)) {
     $form['tac_protected_terms'] = array(
       '#type' => 'value',
-      '#value' => taxonomy_access_preserve_terms($form['#node'])
+      '#value' => taxonomy_access_preserve_terms($form_state['node']),
     );
   }
 }
@@ -323,8 +324,13 @@ function taxonomy_access_form_taxonomy_form_term_alter(&$form, &$form_state) {
  * determine which {node_access} entries must be updated before the
  * {taxonomy_term_data} and {taxonomy_index} records are deleted from the database.
  */
-function taxonomy_access_form_taxonomy_vocabulary_confirm_delete_alter(&$form, &$form_state) {
-  $form['#submit'] = array('taxonomy_access_vocabulary_delete_submit');
+function taxonomy_access_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
+  // The form returned by taxonomy_form_vocabulary() acts as a delete
+  // confirmation form only under some conditions, so we check $form['#id'] to
+  // see which version of the form is actually being used.
+  if ($form['#id'] == 'taxonomy_vocabulary_confirm_delete') {
+    $form['#submit'] = array('taxonomy_access_vocabulary_delete_submit');
+  }
 }
 
 
@@ -337,7 +343,7 @@ function taxonomy_access_form_taxonomy_vocabulary_confirm_delete_alter(&$form, &
  */
 function taxonomy_access_term_submit(&$form, &$form_state) {
   // If we are deleting a term, override the default behavior.
-  if ($form_state['clicked_button']['#value'] == t('Delete')) {
+  if ($form_state['triggering_element']['#value'] == t('Delete')) {
 
     // If the user has already confirmed deletion, proceed.
     if ($form_state['values']['delete'] === TRUE) {
@@ -842,18 +848,17 @@ function _taxonomy_access_get_nodes_for_vocabulary($vid, $rid = NULL) {
  *    controlled for the role.
  */
 function _taxonomy_access_get_nodes_for_role($rid) {
-  $nids = array();
-  $result = db_query(
-    'SELECT n.nid
-     FROM {taxonomy_index} n
-     LEFT JOIN {taxonomy_term_data} d ON n.tid = d.tid
-     LEFT JOIN {term_access} a ON n.tid = a.tid
-     LEFT JOIN {term_access_defaults} ad on ad.vid = d.vid
-     WHERE a.rid = %d OR ad.rid = %d', $rid, $rid);
+  $query = db_select('taxonomy_index', 'n')
+    ->fields('n', array('nid'));
+  $query->leftJoin('taxonomy_term_data', 'd', 'n.tid = d.tid');
+  $query->leftJoin('term_access', 'a', 'n.tid = a.tid');
+  $query->leftJoin('term_access_defaults', 'ad', 'ad.vid = d.vid');
+  $query->condition(db_or()
+    ->condition('a.rid', $rid)
+    ->condition('ad.rid', $rid)
+  );
 
-  while ($node = db_fetch_object($result)) {
-    $nids[] = $node->nid;
-  }
+  $nids = $query->execute()->fetchCol();
   return $nids;
 }
 
@@ -1084,5 +1089,5 @@ function _taxonomy_access_grant_help_table() {
     . "<p>" . t("This does <strong>not</strong> control whether the role can see the <em>nodes</em> listed in Views, only the <em>term</em>.") . "</p>",
   );
 
-  return theme('table', $header, $rows, array('class' => 'grant_help'));
-}
\ No newline at end of file
+  return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('class' => array('grant_help'))));
+}
diff --git taxonomy_access_admin.inc taxonomy_access_admin.inc
index 54b0d7a..4ed4b99 100644
--- taxonomy_access_admin.inc
+++ taxonomy_access_admin.inc
@@ -52,10 +52,10 @@ function theme_taxonomy_access_admin() {
   $header = array(t('Role'), array('data' => '&nbsp;'));
   $rows = array();
 
-  $result = db_query('SELECT rid FROM {term_access_defaults} WHERE vid=0 ');
+  $active_role_ids = db_query('SELECT rid FROM {term_access_defaults} WHERE vid = 0')->fetchCol();
   $active = array();
-  while ($role = db_fetch_array($result)) {
-    $active[$role['rid']] = TRUE;
+  foreach ($active_role_ids as $rid) {
+    $active[$rid] = TRUE;
   }
   foreach ($roles as $rid => $name) {
     $ops = array();
@@ -72,7 +72,7 @@ function theme_taxonomy_access_admin() {
     $rows[] = array($name, array('data' => implode(' | ', $ops), 'align' => 'right'));
   }
 
-  return theme('table', $header, $rows);
+  return theme('table', array('header' => $header, 'rows' => $rows));
 }
 
 /**
@@ -80,21 +80,22 @@ function theme_taxonomy_access_admin() {
  * @param $rid
  *     The role id to disable.
  */
-function taxonomy_access_admin_delete_role($form_state, $rid) {
+function taxonomy_access_admin_delete_role($form, $form_state, $rid) {
   if (is_numeric($rid) AND $rid > 2) {
-    $r = db_query(
+    $role_global_default = db_query(
       'SELECT grant_view, grant_update, grant_delete
        FROM {term_access_defaults} 
-       WHERE vid=0 AND rid=%d', $rid);
-    $role_global_default = db_fetch_array($r);
+       WHERE vid = 0 AND rid = :rid', array(':rid' => $rid))
+      ->fetchAssoc();
     if ($role_global_default) {
       if ($_POST['confirm']) {
 
         // Compare this role's global default node access to auth user role.
-        $auth_global_default = db_fetch_array(db_query(
+        $auth_global_default = db_query(
           'SELECT grant_view, grant_update, grant_delete
            FROM {term_access_defaults} 
-           WHERE vid=0 AND rid=2'));
+           WHERE vid = 0 AND rid = :rid', array(':rid' => DRUPAL_AUTHENTICATED_RID))
+          ->fetchAssoc();
 
         if ($role_global_default == $auth_global_default) {
           $affected_nodes = _taxonomy_access_get_nodes_for_role($rid);
@@ -104,8 +105,7 @@ function taxonomy_access_admin_delete_role($form_state, $rid) {
           // all uncontrolled nodes, so flag node access for rebuild.
           node_access_needs_rebuild(TRUE);
         }
-        db_query('DELETE FROM {term_access} WHERE rid=%d', $rid);
-        db_query('DELETE FROM {term_access_defaults} WHERE rid=%d', $rid);
+        taxonomy_access_delete_role($rid);
         if (!empty($affected_nodes)) {
           _taxonomy_access_node_access_update($affected_nodes);
         }
@@ -165,12 +165,9 @@ function taxonomy_access_admin_build_row($grants = NULL) {
  * @param $rid
  *     The role id.
  */
-function taxonomy_access_admin_form($form_state, $rid = NULL) {
+function taxonomy_access_admin_form($form, $form_state, $rid = NULL) {
   // Fetch all default grants.
-  $result = db_query('SELECT * FROM {term_access_defaults} WHERE rid = %d', $rid);
-  while ($row = db_fetch_array($result)) {
-    $default_grants[$row['vid']] = $row;
-  }
+  $default_grants = db_query('SELECT * FROM {term_access_defaults} WHERE rid = :rid', array(':rid' => $rid))->fetchAllAssoc('vid', PDO::FETCH_ASSOC);
 
   // If we are adding a role, no global default is set yet, so insert it now.
   if (empty($default_grants[0]) && isset($rid)) {
@@ -184,13 +181,10 @@ function taxonomy_access_admin_form($form_state, $rid = NULL) {
   }
 
   // Fetch all grants.
-  $result = db_query('SELECT * FROM {term_access} WHERE rid = %d', $rid);
-  while ($row = db_fetch_array($result)) {
-    $grants[$row['tid']] = $row;
-  }
+  $grants = db_query('SELECT * FROM {term_access} WHERE rid = :rid', array(':rid' => $rid))->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
 
   $form['instructions'] = array(
-    '#value' => _taxonomy_access_admin_instructions_html(),
+    '#markup' => _taxonomy_access_admin_instructions_html(),
     '#weight' => '20',
   );
   $form['rid'] = array('#type' => 'value', '#value' => $rid);
@@ -254,12 +248,14 @@ function taxonomy_access_admin_form($form_state, $rid = NULL) {
     );
   }
 
-  $form['delete'] = array(
+  $form['actions'] = array('#type' => 'actions');
+
+  $form['actions']['delete'] = array(
     '#type' => 'submit',
     '#value' => t('Delete selected'),
   );
 
-  $form['submit'] = array(
+  $form['actions']['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Save all'),
   );
@@ -273,7 +269,9 @@ function taxonomy_access_admin_form($form_state, $rid = NULL) {
  * @return
  *     String containing rendered HTML form in table.
  */
-function theme_taxonomy_access_admin_form($form) {
+function theme_taxonomy_access_admin_form($variables) {
+  $form = $variables['form'];
+
   $roles = _taxonomy_access_user_roles();
   $header = array(
     array( 'data' => t('Category'), 'colspan' => 3),
@@ -355,8 +353,8 @@ function theme_taxonomy_access_admin_form($form) {
 
 
   $output = '';
-  $output .= theme('table', $header, $rows);
-  $output .= drupal_render($form);
+  $output .= theme('table', array('header' => $header, 'rows' => $rows));
+  $output .= drupal_render_children($form);
 
   return $output;
 }
@@ -374,10 +372,10 @@ function taxonomy_access_admin_form_submit($form, &$form_state) {
         foreach ($values['selected_terms'] as $tid => $enabled) {
           if ($enabled) {
             $affected_nodes = _taxonomy_access_get_nodes_for_term($tid);
-            db_query(
-              'DELETE FROM {term_access} WHERE rid = %d AND tid = %d',
-              $values['rid'], $tid
-            );
+            db_delete('term_access')
+              ->condition('rid', $values['rid'])
+              ->condition('tid', $tid)
+              ->execute();
             _taxonomy_access_cache_affected_nodes($affected_nodes);
           }
         }
@@ -387,11 +385,10 @@ function taxonomy_access_admin_form_submit($form, &$form_state) {
           if ($enabled) {
             $affected_nodes =
               _taxonomy_access_get_nodes_for_vocabulary($vid, $values['rid']);
-            db_query(
-              "DELETE FROM {term_access_defaults} WHERE rid = %d AND vid = %d",
-              $values['rid'],
-              $vid
-            );
+            db_delete('term_access_defaults')
+              ->condition('rid', $values['rid'])
+              ->condition('vid', $vid)
+              ->execute();
             _taxonomy_access_cache_affected_nodes($affected_nodes);
           }
         }
@@ -466,7 +463,7 @@ function taxonomy_access_admin_form_submit($form, &$form_state) {
         _taxonomy_access_node_access_update($affected_nodes);
       }
 
-      drupal_goto('admin/user/taxonomy_access');
+      $form_state['redirect'] = 'admin/user/taxonomy_access';
       break;
   }
 }
@@ -512,7 +509,10 @@ function taxonomy_access_grant_update($tid, $rid = NULL, $grants = NULL, $skip_n
   }
 
   // Delete old entries.
-  db_query("DELETE FROM {term_access} WHERE tid=%d AND rid=%d", $tid, $rid);
+  db_delete('term_access')
+    ->condition('tid', $tid)
+    ->condition('rid', $rid)
+    ->execute();
 
   // Insert new entries.
   drupal_write_record('term_access', $row);
@@ -551,11 +551,11 @@ function taxonomy_access_recursive_grant_update($tid, $rid = NULL, $grants = NUL
         $run_tids = array(); // stop the execution
       }
       else {
-        $result = db_query('SELECT th.tid FROM {taxonomy_term_hierarchy} th WHERE th.parent = %d', $run_tid);
+        $child_tids = db_query('SELECT th.tid FROM {taxonomy_term_hierarchy} th WHERE th.parent = :run_tid', array(':run_tid' => $run_tid))->fetchCol();
         // If this tid has children, update grants and queue the children
-        while ($row = db_fetch_array($result)) {
-          taxonomy_access_grant_update($row['tid'], $rid, $grants);
-          $run_tids[] = $row['tid'];
+        foreach ($child_tids as $child_tid) {
+          taxonomy_access_grant_update($child_tid, $rid, $grants);
+          $run_tids[] = $child_tid;
         }
 
         // Remove this tid from the queue and mark as processed,
@@ -613,7 +613,10 @@ function taxonomy_access_defaults_update($vid, $rid = NULL, $grants = NULL, $ski
   }
 
   // Delete old entries.
-  db_query("DELETE FROM {term_access_defaults} WHERE vid = %d AND rid = %d", $vid, $rid);
+  db_delete('term_access_defaults')
+    ->condition('vid', $vid)
+    ->condition('rid', $rid)
+    ->execute();
 
   // Insert new entries.
   drupal_write_record('term_access_defaults', $row);
