diff -rupN autocategorise/autocategorise.install autocategorise_p/autocategorise.install
--- autocategorise/autocategorise.install	2009-06-10 09:20:34.000000000 +1000
+++ autocategorise.install	2010-01-27 16:00:03.000000000 +1100
@@ -1,4 +1,10 @@
 <?php
+// $Id$
+
+/**
+ * @file
+ * Installation file for autocategorise module.
+ */
 
 /**
  * Implementation of hook_install().
@@ -9,15 +15,16 @@ function autocategorise_install() {
 
 /**
  * Implementation of hook_uninstall().
- * http://api.drupal.org/api/function/hook_uninstall/6
- * Remove any tables or variables that the module sets.
  */
 function autocategorise_uninstall() {
-  variable_del('autocategorise_vids', array());
+  variable_del('autocategorise_vids');
 }
 
+/**
+ * Implementation of hook_update_N().
+ */
 function autocategorise_update_1() {
   $var = variable_get('auto_vids', array());
   variable_set('autocategorise_vids', $var);
   return array();
-}
\ No newline at end of file
+}
diff -rupN autocategorise/autocategorise.module autocategorise_p/autocategorise.module
--- autocategorise/autocategorise.module	2009-07-30 05:48:46.000000000 +1000
+++ autocategorise.module	2010-01-27 17:11:08.524852123 +1100
@@ -3,65 +3,68 @@
 
 /**
  * @file
-Users cannot be trusted to categorise things themselves, and it's too much work for administrators. 
-The vocabulary terms make up the sections in the 'yellow pages', while the synonyms are used to make matches. Synonyms can be words or word stems. If a vocabulary is 'required', then the heaviest term will be used as a catch-all, or misc.
-The module provides a checkbox in the vocabulary edit settings fieldset, which puts the module into action when a node is submitted. The synonyms in the vocabulary iterate through the the title and body of the node looking for a match or matches.
-The module also provides a button on that page, which will recategorise all the content in the governed types. This means the whole category system is flexible and can evolve and be tweaked.
+ * Users cannot be trusted to categorise things themselves, and it's too much work for administrators. 
+ * The vocabulary terms make up the sections in the 'yellow pages', while the synonyms are used to make matches.
+ * Synonyms can be words or word stems. If a vocabulary is 'required', then the heaviest term will be used as a catch-all, or misc.
+ * The module provides a checkbox in the vocabulary edit settings fieldset, which puts the module into action when a node is submitted.
+ * The synonyms in the vocabulary iterate through the the title and body of the node looking for a match or matches.
+ * The module also provides a button on that page, which will recategorise all the content in the governed types.
+ * This means the whole category system is flexible and can evolve and be tweaked.
  */
  
 /**
- * Implementation of hook_help)().
- * http://api.drupal.org/api/function/hook_help/6
+ * Implementation of hook_help().
  */
-function autocategorise_module_help($section) {
-  switch ($section) {
+function autocategorise_help($path, $arg) {
+  switch ($path) {
     case 'admin/help#search':
-      return '<p>'. t('Allows contentTypes to be automatically categorised by a vocabulary, either on submission, or when prompted on the settings page') .'</p>';
+      return '<p>' . t('Allows contentTypes to be automatically categorised by a vocabulary, either on submission, or when prompted on the settings page') . '</p>';
     case 'admin/content/taxonomy/autocategorise':
-      return '<p>'. t('For each ContentType listed here, there is a button ') .'</p>';
+      return '<p>' . t('For each ContentType listed here, there is a button ') . '</p>';
   }
 }
 
 /**
- * Implementation of hook_form_alter)().
- * http://api.drupal.org/api/function/hook_form_alter/6
- * Perform alterations before a form is rendered.
+ * Implementation of hook_form_alter().
  */
 function autocategorise_form_alter(&$form, $form_state, $form_id) {
-  //on the contentType_node_form, we need to remove the taxonomy fields for autovocabs
-  if (strpos($form_id, '_node_form')){
+  // On the contentType_node_form, we need to remove the taxonomy fields for autovocabs
+  if (strpos($form_id, '_node_form')) {
     $all_autovocabs = variable_get('autocategorise_vids', array());
-    if (count($all_autovocabs)){
-      foreach ($all_autovocabs as $vid => $val){
-        if ($val)unset($form['taxonomy'][$vid]);
+    if (count($all_autovocabs)) {
+      foreach ($all_autovocabs as $vid => $val) {
+        if ($val) {
+          unset($form['taxonomy'][$vid]);
+        }
       }
     }
   }
-  
-  //reminds users that even with autocategorisations, they still need to apply the vocabulary to the contentType on the vocabulary settings page
-  else if ($form_id == "taxonomy_form_vocabulary"){
-    $autoSetting=is_auto_vocab(arg(5));
-    $form['settings']['auto']= array(
+  // Reminds users that even with autocategorisations, they still need to apply the
+  // vocabulary to the contentType on the vocabulary settings page
+  elseif ($form_id == "taxonomy_form_vocabulary") {
+    $auto_setting = autocategorise_is_auto_vocab(arg(5));
+    $form['settings']['auto'] = array(
       '#type' => 'checkbox',
       '#title' => 'Apply automatically to contentTypes listed above',
-      '#default_value' => $autoSetting,
-      '#description' => ' '. t('When a node is submitted this vocabulary will scan it and match the node title and body against the term sysnonyms.')
+      '#default_value' => $auto_setting,
+      '#description' => ' '. t('When a node is submitted this vocabulary will scan it and match the node title and body against the term sysnonyms.'),
     );
-    $form['autocategorise']=array(
-      '#type'=>'submit',
-      '#value'=>'autocategorise'
+    $form['autocategorise'] = array(
+      '#type' => 'submit',
+      '#value' => 'autocategorise',
     );
     $form['settings']['required']['#description'] .= t('Autocategorise will assume that the last, heaviest term is the <em>misc</em>, or <em>other</em> category');
-    $form['#submit'][] ='autocategorise_taxonomy_form_vocabulary_submit';
+    $form['#submit'][] = 'autocategorise_taxonomy_form_vocabulary_submit';
   }
-  
-  //next to the synonym field, users are reminded what to put in there.
-  elseif ($form_id == "taxonomy_form_term"){
-    if (!is_auto_vocab($form['#vocabulary']['vid'])) return;
-    //move the synonyms out of the advnaced fieldset 
+  // Next to the synonym field, users are reminded what to put in there.
+  elseif ($form_id == "taxonomy_form_term") {
+    if (!autocategorise_is_auto_vocab($form['#vocabulary']['vid'])) {
+      return;
+    }
+    // Move the synonyms out of the advnaced fieldset 
     $form['synonyms'] = $form['advanced']['synonyms'];
-    unset ($form['advanced']['synonyms']);
-    //the other items don't have weight, so we need to add weights to place the synonyms
+    unset($form['advanced']['synonyms']);
+    // The other items don't have weight, so we need to add weights to place the synonyms
     $form['advanced']['#weight'] = 2;
     $form['submit']['#weight'] = 3;
     $form['delete']['#weight'] = 3;
@@ -71,88 +74,108 @@ function autocategorise_form_alter(&$for
 } 
  
 /**
- * Implementation of hook_nodeapi)().
- * http://api.drupal.org/api/function/hook_nodeapi/6
+ * Implementation of hook_nodeapi().
  */
-function autocategorise_nodeapi($node, $op) {
-  if ($op=='insert' || $op=='update'){
-    //get all the vocabs which govern this node 
+function autocategorise_nodeapi($node, $op, $a3 = NULL, $a4 = NULL) {
+  if ($op == 'insert' || $op == 'update') {
+    // Get all the vocabs which govern this node 
     $vocabs = taxonomy_get_vocabularies($node->type);
-    if (!count($vocabs)) return;
+    if (!count($vocabs)) {
+      return;
+    }
     foreach ($vocabs as $vocab) {
-      if (is_auto_vocab($vocab->vid)){
-        $terms = get_array_for_matching($vocab->vid);
-        $misc=_apply_vocab_to_node($node, $terms, $vocab);
+      if (autocategorise_is_auto_vocab($vocab->vid)) {
+        $terms = autocategorise_get_array_for_matching($vocab->vid);
+        $misc = _autocategorise_apply_vocab_to_node($node, $terms, $vocab);
       }
     }
   }
 }
 
-//process the auto-checkbox on admin/content/taxonomy/edit/vocabulary/
-function autocategorise_taxonomy_form_vocabulary_submit($form, &$form_state){
-  //update the variable containing a list of of autovocabs
-  $vArray=variable_get('autocategorise_vids', array());
-  $vArray[$form['vid']['#value']] = $form['settings']['auto']['#value'];
-  variable_set('autocategorise_vids', $vArray);
-  //if the autocategorise button was pressed then remove all tags from this vocab, and recategorise
-  if ($form['#post']['op']=='autocategorise'){
-    if ($types = $form['#post']['nodes']){
-      autocategorise_vocab($form['vid']['#value'], $types);
-    }else{
+/**
+ * Additional submit handler for taxonomy_form_vocabulary page.
+ * Process the auto-checkbox.
+ */
+function autocategorise_taxonomy_form_vocabulary_submit($form, &$form_state) {
+  // Update the variable containing a list of autovocabs
+  $vocabs = variable_get('autocategorise_vids', array());
+  $vocabs[$form_state['values']['vid']] = $form_state['values']['auto'];
+  variable_set('autocategorise_vids', $vocabs);
+  // If the autocategorise button was pressed then remove all tags from this vocab, and recategorise
+  if ($form_state['clicked_button']['#value'] == 'autocategorise') {
+    if ($types = $form_state['values']['nodes']) {
+      autocategorise_vocab($form_state['values']['vid'], $types);
+    }
+    else {
       drupal_set_message(t('You need to select some contentTypes for this vocabulary before you can autocategorise'));
     }
   } 
 }
 
-//goes through every node in contentTypes governed by the autovocab
-function autocategorise_vocab($vid, $types){
-  //get all the nodes for the contentType(s)
-  $terms = get_array_for_matching($vid);
-  foreach ($types as $type){
-    $condition[]= " type = '$type' ";
-  }
-  $results = db_query("SELECT nid FROM {node} WHERE " . implode(" OR ", $condition));
-
-  while ($nid = db_result($results)){
+/**
+ * Goes through every node in contentTypes governed by the autovocab
+ */
+function autocategorise_vocab($vid, $types) {
+  // Get all the nodes for the contentType(s)
+  $terms = autocategorise_get_array_for_matching($vid);
+  $in = '';
+  foreach ($types as $type) {
+    $in .= $in ? ", '%s'" : "'%s'";
+    $sql_args[] = $type;
+  }
+  $results = db_query("SELECT nid FROM {node} WHERE type IN ($in)", $sql_args);
+  $nids = array();
+  while ($nid = db_result($results)) {
     $nids[] = $nid;
   }
-  if (!count($nids)) return;
+  if (!count($nids)) {
+    return;
+  }
   $vocabs = taxonomy_get_vocabularies();
-  foreach ($nids as $nid){
-    //by loading the nodes in this loop and not before, they won't clog up the RAM
-    $node = node_load(array('nid' => $nid));
-    _apply_vocab_to_node($node, $terms, $vocabs[$vid]);
+  foreach ($nids as $nid) {
+    // By loading the nodes in this loop and not before, they won't clog up the RAM
+    $node = node_load($nid);
+    _autocategorise_apply_vocab_to_node($node, $terms, $vocabs[$vid]);
   }
 }
 
-function _apply_vocab_to_node($node, $terms, $vocab){
+/**
+ * Helper function to get the terms for a node and save them to the node.
+ */
+function _autocategorise_apply_vocab_to_node($node, $terms, $vocab) {
   if (!count($terms)) {
-   drupal_set_message("Vocabulary $vid has no terms in it");
-   return; 
+    drupal_set_message(t("Vocabulary %vid has no terms in it", array('%vid' => $vid)));
+    return; 
   }
-  
+
   // Append title to body of node 
-  $haystack = strtolower($node->title .' '. $node->body);
-  
+  $haystack = strtolower($node->title . ' ' . $node->body);
+
   foreach ($terms as $tid => $synonyms) {
     foreach ($synonyms as $synonym) {
       //This will compare parts of words using strpos instead of whole words
-      if ( is_int(strpos($haystack, strtolower($synonym)))){
+      if (is_int(strpos($haystack, strtolower($synonym)))) {
         $matched_nids[] = $tid;
         break 1;
       }
     }
-    if (count($matched_nids) && !$vocab->multiple ) break;
+    if (count($matched_nids) && !$vocab->multiple) {
+      break;
+    }
   }
-  if ( empty( $matched_nids ) ){
-    //this will be the last term in terms, which is used as a catch all
+  if (empty($matched_nids)) {
+    // This will be the last term in terms, which is used as a catch all
     $matched_nids[] = $tid;
   }
-  //tax_node save deletes all the terms applied to this node, and reapplies them
-  //so we have to get all the current terms
-  //this is very similar to $node->taxonomy but the format of that variable seems to vary according to circumstances.
-  $result = db_query("SELECT d.tid from {term_node} n LEFT JOIN {term_data} d on n.tid = d.tid WHERE n.nid = %d AND d.vid <> %d", $node->nid, $vocab->vid);
-  while ($tid = db_result($result) ){
+  // tax_node save deletes all the terms applied to this node, and reapplies them
+  // So we have to get all the current terms
+  // This is very similar to $node->taxonomy but the format of that variable seems to vary according to circumstances.
+  $result = db_query("SELECT d.tid
+                      FROM {term_node} n
+                      LEFT JOIN {term_data} d ON n.tid = d.tid
+                      WHERE n.nid = %d
+                      AND d.vid <> %d", $node->nid, $vocab->vid);
+  while ($tid = db_result($result)) {
     $other_tids_from_other_vids[] = $tid;
   }
   if (count($other_tids_from_other_vids)) {
@@ -161,8 +184,10 @@ function _apply_vocab_to_node($node, $te
   taxonomy_node_save($node, array_unique($matched_nids));
 }
 
-//returns an array with terms and their synonyms indexed by tid
-function get_array_for_matching($vid) {
+/**
+ * Returns an array with terms and their synonyms indexed by tid
+ */
+function autocategorise_get_array_for_matching($vid) {
   $term_object_array = taxonomy_get_tree($vid);
   foreach ($term_object_array as $term) {
     $tid = $term->tid;
@@ -178,12 +203,14 @@ function get_array_for_matching($vid) {
     return $synonyms;
   }
   else {
-    form_set_error(t('Autocategorise cannot continue with no terms in vocabulary '.$vid));
+    drupal_set_message(t('Autocategorise cannot continue with no terms in vocabulary %vocab', array('%vocab' => $vid)), 'error');
   }
 }
 
-//checks to see if a vocabulary is defined as automatic
-function is_auto_vocab($vid){
-  $autoVocabs= variable_get('autocategorise_vids', array());
-  return $autoVocabs[$vid];
-}
\ No newline at end of file
+/**
+ * Checks to see if a vocabulary is defined as automatic
+ */
+function autocategorise_is_auto_vocab($vid) {
+  $auto_vocabs = variable_get('autocategorise_vids', array());
+  return $auto_vocabs[$vid];
+}
