diff --git a/inactive_user.install b/inactive_user.install
index ffaa10b..997b6e2 100644
--- a/inactive_user.install
+++ b/inactive_user.install
@@ -97,7 +97,8 @@ function inactive_user_uninstall() {
   variable_del('inactive_user_preserve_content');
   variable_del('inactive_user_timestamp');
   $settings = db_query("SELECT name FROM {variable} WHERE name LIKE 'inactive_user_%'");
-  while ($row = db_fetch_object($settings)) {
+  $record = $settings->fetchObject();
+  while ($row = $record) {
     variable_del($row->name);
   }
   drupal_set_message(t('Inactive user has been uninstalled.'));
diff --git a/inactive_user.module b/inactive_user.module
index 9f85f1b..03e3f5f 100644
--- a/inactive_user.module
+++ b/inactive_user.module
@@ -8,28 +8,23 @@
  * The user can configure the time after Drupal sends mails.
  */
 
-
+ 
 /**
  * Implements hook_permission().
  */
 function inactive_user_permission() {
-  return array('change inactive user settings');
-}
-
-/**
- * Implements hook_user_cancel().
- */
-function inactive_user_user_cancel($edit, $account, $method) {
-  db_delete('inactive_users')
-  ->condition('uid', $account->uid)
-  ->execute();
+  return array(
+    'change inactive user settings' => array(
+      'title' => t('Change inactive user settings'), 
+    ),
+  );
 }
 
 /**
  * Implements hook_menu().
  */
 function inactive_user_menu() {
-  $items['admin/user/inactive-user'] = array(
+  $items['admin/people/inactive-user'] = array(
     'title' => 'Inactive users',
     'description' => 'Set rules and contact templates for inactive users.',
     'page callback' => 'drupal_get_form',
@@ -42,14 +37,23 @@ function inactive_user_menu() {
 }
 
 /**
+ * Implements hook_user_cancel().
+ */
+function inactive_user_user_cancel($edit, $account, $method) {
+  db_delete('inactive_users')
+  ->condition('uid', $account->uid)
+  ->execute();
+}
+
+/**
  * Custom settings page: menu callback
  *
  * (we're using a custom callback to enable a nicer menu title,
  * without underscore)
  */
 function inactive_user_custom_settings() {
-  $period = array(0 => 'disabled') + drupal_map_assoc(array(604800, 1209600, 1814400, 2419200, 2592000, 7776000, 15552000, 23328000, 31536000, 47088000, 63072000), '_format_interval');
-  $warn_period = array(0 => 'disabled') + drupal_map_assoc(array(86400, 172800, 259200, 604800, 1209600, 1814400, 2592000), '_format_interval');
+  $period = array(0 => 'disabled') + drupal_map_assoc(array(604800, 1209600, 1814400, 2419200, 2592000, 7776000, 15552000, 23328000, 31536000, 47088000, 63072000), 'format_interval');
+  $warn_period = array(0 => 'disabled') + drupal_map_assoc(array(86400, 172800, 259200, 604800, 1209600, 1814400, 2592000), 'format_interval');
   $mail_variables = ' %username, %useremail, %lastaccess, %period, %sitename, %siteurl';
 
   // set administrator e-mail
@@ -211,7 +215,7 @@ function inactive_user_custom_settings() {
   return system_settings_form($form);
 }
 
-function inactive_user_custom_settings_validate($form, &$form_state) {
+function inactive_user_validate($form, &$form_state) {
   $mails = explode(',', $edit['inactive_user_admin_email']);
   foreach ($mails as $mail) {
     if ($mail && !valid_email_address(trim($mail))) {
@@ -234,32 +238,50 @@ function inactive_user_cron() {
   if ((REQUEST_TIME - variable_get('inactive_user_timestamp', '0')) >= 86100) { // Only check once every almost-day, so we slide around the clock and don't overload the server.
     variable_set('inactive_user_timestamp', REQUEST_TIME);
     $user_list = '';
-
-    // reset notifications if recent user activity
-    $users = db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid <> :uid', array(':uid' => 1)));
-    if ($users) {
-      foreach ($users as $uid) {
-        $u = db_fetch_object(db_query('SELECT access, name FROM {users} WHERE uid = :uid', array(':uid' => $uid)));
-        if ($u->access > REQUEST_TIME - 604800) {
+    
+    // Select all user ids from inactive_users excluding user one
+    $users = db_query('SELECT uid FROM {inactive_users} WHERE uid <> :one', array(':one' => 1));
+    $record = $users->fetchObject();
+    if ($record) {
+      foreach ($record as $uid) {
+        $u = db_query('SELECT access, name FROM {users} WHERE uid = :uid', array(':uid' => $uid));
+        $u_object = $u->fetchObject();
+        if ($u_object->access > REQUEST_TIME - 604800) {
           // user activity in last week, remove from inactivity table
-          db_query('DELETE FROM {inactive_users} WHERE uid = %d', $uid);
           db_delete('inactive_users')
             ->condition('uid', $uid)
             ->execute();
-          watchdog('user', 'recent user activity: %user removed from inactivity list', array('%user' => $u->name), WATCHDOG_NOTICE, l(t('edit user'), "user/$uid/edit", array('query' => array('destination' => 'admin/user/user'))));
+          watchdog('user', 'recent user activity: %user removed from inactivity list', array('%user' => $u_object->name), WATCHDOG_NOTICE, l(t('edit user'), "user/" . $uid . "/edit", array('query' => array('destination' => 'admin/user/user'))));
         }
       }
     }
 
     // notify administrator of inactive user accounts
     if ($notify_time = variable_get('inactive_user_notify_admin', 0)) {
-      $result = db_query('SELECT uid, name, mail, access, created FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', REQUEST_TIME, $notify_time, REQUEST_TIME, $notify_time);
-      while ($user = db_fetch_object($result)) {
-        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND notified_admin = 1', $user->uid)) && ($user->created < (REQUEST_TIME - $notify_time))) {
-          db_query('UPDATE {inactive_users} SET notified_admin = 1 WHERE uid = %d', $user->uid);
-          if (!db_affected_rows()) {
+      $result = db_query('SELECT uid, name, mail, access, created FROM {users} 
+                WHERE ((access <> :zero AND login <> :zero AND access < (:request_time - :notify_time)) 
+                OR (login = :zero AND created < (:request_time - :notify_time))) AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':notify_time' => $notify_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();
+      while ($user = $record) {
+        $a = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND notified_admin = :one', array(
+          ':uid' => $user->uid,
+          ':one' => 1,
+        ));
+        $b = $a->fetchObject();  
+        if ($user->uid && !$b && ($user->created < (REQUEST_TIME - $notify_time))) {
+         $query = db_update('inactive_users');
+          $query->fields(array('notified_admin' => 1, 'uid' => $user->uid));
+          $result = $query->execute();
+          if (!$result) {
             // must create a new row
-            @db_query('INSERT INTO {inactive_users} (uid, notified_admin) VALUES (%d, 1)', $user->uid);
+            db_insert('inactive_users')
+            ->fields(array('uid' => $user->uid, 'notified_admin' => 1))
+            ->execute();
           }
           $user_list .= "$user->name ($user->mail) last active on " . format_date($user->access, 'large') . ".\n";
         }
@@ -272,109 +294,221 @@ function inactive_user_cron() {
 
     // notify users that their account has been inactive
     if ($notify_time = variable_get('inactive_user_notify', 0)) {
-      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', REQUEST_TIME, $notify_time, REQUEST_TIME, $notify_time);
-      while ($user = db_fetch_object($result)) {
-        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE notified_user = 1 AND uid = %d', $user->uid)) && ($user->created < (REQUEST_TIME - $notify_time))) {
-          db_query('UPDATE {inactive_users} SET notified_user = 1 WHERE uid = %d', $user->uid);
-          if (!db_affected_rows()) {
-            @db_query('INSERT INTO {inactive_users} (uid, notified_user) VALUES (%d, 1)', $user->uid);
+      $result = db_query('SELECT * FROM {users} WHERE ((access <> :zero AND login <> :zero 
+                AND access < (:request_time - :notify_time)) OR (login = :zero AND created < (:request_time - :notify_time))) 
+                AND status <> :zero AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':notify_time' => $notify_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();
+      while ($user = $record) {
+        $e = db_query('SELECT uid FROM {inactive_users} WHERE notified_user = :one AND uid = :uid', array(
+          'one' => 1,
+          ':uid' => $user->uid,
+        ));
+        $f = $e->fetchObject();
+        if ($user->uid && !$f && ($user->created < (REQUEST_TIME - $notify_time))) {
+          $query = db_update('inactive_users');
+          $query->fields(array('notified_user' => 1, 'uid' => $user->uid));
+          $result = $query->execute();
+          $h = $g->rowCount();
+          if (!$result) {
+            // must create a new row
+            db_insert('inactive_users')
+            ->fields(array('uid' => $user->uid,'notified_user' => 1))
+            ->execute();
           }
           _inactive_user_mail(t('[@sitename] Account inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_notify_text', _inactive_user_mail_text('notify_text')), $notify_time, $user, NULL);
-          watchdog('user', 'user %user notified of inactivity', array('%user' => $user->name), WATCHDOG_INFO, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
+          //watchdog('user', 'user %user notified of inactivity', array('%user' => $user->name), WATCHDOG_INFO, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
+          watchdog('user', 'user %user notified of inactivity', array('%user' => $user->name), WATCHDOG_INFO, l(t('edit user'), "user/" . $user->uid . "/edit", array('query' => array('destination' => 'admin/user/user'))));
         }
       }
     }
 
     // warn users when they are about to be blocked
-    if (($warn_time = variable_get('inactive_user_auto_block_warn', 0)) &&
-        ($block_time = variable_get('inactive_user_auto_block', 0))) {
-      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', REQUEST_TIME, $warn_time, REQUEST_TIME, $warn_time);
-      while ($user = db_fetch_object($result)) {
-        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_block_timestamp > 0', $user->uid)) && ($user->created < (REQUEST_TIME - $warn_time))) {
-          db_query('UPDATE {inactive_users} SET warned_user_block_timestamp = %d WHERE uid = %d', REQUEST_TIME + $warn_time, $user->uid);
-          if (!db_affected_rows()) {
-            @db_query('INSERT INTO {inactive_users} (uid, warned_user_block_timestamp) VALUES (%d, %d)', $user->uid, REQUEST_TIME + $warn_time);
+    if (($warn_time = variable_get('inactive_user_auto_block_warn', 0)) && ($block_time = variable_get('inactive_user_auto_block', 0))) {
+      $result = db_query('SELECT * FROM {users} WHERE ((access <> :zero AND login <> :zero 
+                AND access < (:request_time - :warn_time)) OR (login = :zero 
+                AND created < (:request_time - :warn_time))) 
+                AND status <> :zero AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':warn_time' => $warn_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();
+      while ($user = $record) {
+          $i = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND warned_user_block_timestamp > :zero', array(
+            ':uid' => $user->uid,
+            ':zero' => 0,
+          ));
+          $j = $i->fetchObject();
+        if ($user->uid && !$j && ($user->created < (REQUEST_TIME - $warn_time))) {
+          //db_query('UPDATE {inactive_users} SET warned_user_block_timestamp = %d WHERE uid = %d', REQUEST_TIME + $warn_time, $user->uid);
+          $k = db_query('UPDATE {inactive_users} SET warned_user_block_timestamp = :request_warn WHERE uid = :uid', array(
+            ':request_warn' => REQUEST_TIME + $warn_time, 
+            ':uid' => $user->uid,
+          ));
+          $l = $k->rowCount();
+          if (!$l) {
+            // must create a new row
+            db_insert('inactive_users')
+            ->fields(array('warned_user_block_timestamp' => REQUEST_TIME + $warn_time, 'uid' => $user->uid))
+            ->execute();
           }
           _inactive_user_mail(t('[@sitename] Account inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_block_warn_text', _inactive_user_mail_text('block_warn_text')), $warn_time, $user, NULL);
-          watchdog('user', 'user %user warned will be blocked due to inactivity', array('%user' => $user->name), WATCHDOG_NOTICE, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
+          watchdog('user', 'user %user warned will be blocked due to inactivity', array('%user' => $user->name), WATCHDOG_NOTICE, l(t('edit user'), "user/" . $user->uid . "/edit", array('query' => array('destination' => 'admin/user/user'))));
         }
       }
     }
 
-    // automatically block users
+    // automatically block users 
     if ($block_time = variable_get('inactive_user_auto_block', 0)) {
-      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', REQUEST_TIME, $block_time, REQUEST_TIME, $block_time);
-      while ($user = db_fetch_object($result)) {
+      $result = db_query('SELECT * FROM {users} WHERE ((access <> :zero AND login <> :zero 
+                AND access < (:request_time - :block_time)) OR (login = :zero AND created < (:request_time - :block_time))) 
+                AND status <> :zero AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':block_time' => $block_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();
+      while ($user = $record) {
         // don't block user yet if we sent a warning and it hasn't expired
-        if ($user->uid && db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_block_timestamp < %d', $user->uid, REQUEST_TIME)) && ($user->created < (REQUEST_TIME - $block_time))) {
-          db_query('UPDATE {users} SET status = 0 WHERE uid = %d', $user->uid);
-
-          // notify user
-          if (variable_get('inactive_user_notify_block', 0)) {
-            if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND notified_user_block = 1', $user->uid))) {
-              db_query('UPDATE {inactive_users} SET notified_user_block = 1 WHERE uid = %d', $user->uid);
-              if (!db_affected_rows()) {
-                @db_query('INSERT INTO {inactive_users} (uid, notified_user_block) VALUES (%d, 1)', $user->uid);
-              }
-              _inactive_user_mail(t('[@sitename] Account blocked due to inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_block_notify_text', _inactive_user_mail_text('block_notify_text')), $block_time, $user, NULL);
-              watchdog('user', 'user %user blocked due to inactivity', array('%user' => $user->name), WATCHDOG_NOTICE, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
-            }
-          }
-
-          // notify admin
-          if (variable_get('inactive_user_notify_block_admin', 0)) {
-            if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d and notified_admin_block = 1', $user->uid))) {
-              db_query('UPDATE {inactive_users} SET notified_admin_block = 1 WHERE uid = %d', $user->uid);
-              if (!db_affected_rows()) {
-                @db_query('INSERT INTO {inactive_users} (uid, notified_admin_block) VALUES(%d, 1)', $user->uid);
-              }
-              $user_list .= "$user->name ($user->mail) last active on " . format_date($user->access, 'large') . ".\n";
+        $m = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND warned_user_block_timestamp < :request_time', array(
+          ':uid' => $user->uid, 
+          ':request_time' => REQUEST_TIME,
+        ));
+        $n = $m->fetchObject();
+        if ($user->uid && $n && ($user->created < (REQUEST_TIME - $block_time))) {
+         $query = db_update('users');
+          $query->fields(array('status' => 0, 'uid' => $user->uid));
+          $result = $query->execute();
+        }
+    
+        // notify user
+        if (variable_get('inactive_user_notify_block', 0)) {
+          $result = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND notified_user_block = :one', array(
+            ':uid' => $user->uid,
+            ':one' => 1,
+          ));
+          $record = $result->fetchObject();
+          if (!$record) {
+            $query = db_update('inactive_users');
+            $query->fields(array('notified_user_block' => 1, 'uid' => $user->uid));
+            $result = $query->execute();
+            if (!$result) {
+              // must create a new row
+              db_insert('inactive_users')
+              ->fields(array('uid' => $user->uid, 'notified_user_block' => 1))
+              ->execute();
             }
+              _inactive_user_mail(t('[@sitename] Account blocked due to inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_block_notify_text', _inactive_user_mail_text('block_notify_text')), $block_time, $user, NULL);
+              watchdog('user', 'user %user blocked due to inactivity', array('%user' => $user->name), WATCHDOG_NOTICE, l(t('edit user'), "user/" . $user->uid . "/edit", array('query' => array('destination' => 'admin/user/user'))));
           }
         }
-        if ($user_list) {
-          _inactive_user_mail(t('[@sitename] Blocked users', array('@sitename' => variable_get('site_name', 'drupal'))), _inactive_user_mail_text('block_notify_admin_text'), $block_time, NULL, $user_list);
-          unset($user_list);
+      
+        // notify admin
+        if (variable_get('inactive_user_notify_block_admin', 0)) {
+          $result = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND notified_admin_block = :one', array(
+            ':uid' => $user->uid,
+            ':one' => 1,
+          ));
+          $record = $result->fetchObject();
+          if (!$record) {
+            $query = db_update('inactive_users');
+            $query->fields(array('notified_admin_block' => 1, 'uid' => $user->uid));
+            $result = $query->execute();
+            if (!$result) {
+              db_insert('inactive_users')
+              ->fields(array('uid' => $user->uid, 'notified_admin_block' => 1))
+              ->execute();
+            }              
+          $user_list .= "$user->name ($user->mail) last active on " . format_date($user->access, 'large') . ".\n";
+          }
         }
       }
+      if ($user_list) {
+        _inactive_user_mail(t('[@sitename] Blocked users', array('@sitename' => variable_get('site_name', 'drupal'))), _inactive_user_mail_text('block_notify_admin_text'), $block_time, NULL, $user_list);
+        unset($user_list);
+      }
     }
-
+      
     // warn users when they are about to be deleted
-    if (($warn_time = variable_get('inactive_user_auto_delete_warn', 0)) &&
-        ($delete_time = variable_get('inactive_user_auto_delete', 0))) {
-      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', REQUEST_TIME, $warn_time, REQUEST_TIME, $warn_time);
-      while ($user = db_fetch_object($result)) {
-        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_delete_timestamp > 0', $user->uid)) && ($user->created < (REQUEST_TIME - $warn_time))) {
+    if (($warn_time = variable_get('inactive_user_auto_delete_warn', 0)) && ($delete_time = variable_get('inactive_user_auto_delete', 0))) {
+      $result = db_query('SELECT * FROM {users} WHERE ((access <> :zero AND login <> :zero 
+                AND access < (:request_time - :warn_time)) OR 
+                (login = :zero AND created < (:request_time - :warn_time))) AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':warn_time' => $warn_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();       
+      while ($user = $record) {
+         $q = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND warned_user_delete_timestamp > :zero', $user->uid, array(
+            ':uid' => $user->uid,
+            ':zero' => 0,
+          ));
+          $r = $q->fetchObject();
+        if ($user->uid && !$r && ($user->created < (REQUEST_TIME - $warn_time))) {
           if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {
             $protected = 1;
           }
           else {
             $protected = 0;
           }
-          db_query('UPDATE {inactive_users} SET warned_user_delete_timestamp = %d AND protected = %d WHERE uid = %d', REQUEST_TIME + $warn_time, $protected, $user->uid);
-          if (!db_affected_rows()) {
-            @db_query('INSERT INTO {inactive_users} (uid, warned_user_delete_timestamp, protected) VALUES (%d, %d, %d)', $user->uid, REQUEST_TIME + $warn_time, $protected);
+         $query = db_update('inactive_users');
+          $query->fields(array('warned_user_delete_timestamp' => REQUEST_TIME + $warn_time, 'uid' => $user->uid));
+          $result = $query->execute();
+          if (!$result) {
+            // must create a new row
+            db_insert('inactive_users')
+            ->fields(array('uid' => $user->uid, 'warned_user_delete_timestamp' => REQUEST_TIME + $warn_time,'protected' => $protected))
+            ->execute();
           }
           if (!$protected) {
             _inactive_user_mail(t('[@sitename] Account inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_delete_warn_text', _inactive_user_mail_text('delete_warn_text')), $warn_time, $user, NULL);
-            watchdog('user', 'user %user warned will be deleted due to inactivity', array('%user' => $user->mail), WATCHDOG_NOTICE, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
+            watchdog('user', 'user %user warned will be deleted due to inactivity', array('%user' => $user->mail), WATCHDOG_NOTICE, l(t('edit user'), "user/" . $user->uid . "/edit", array('query' => array('destination' => 'admin/user/user'))));
           }
         }
       }
     }
-
+    
     // automatically delete users
     if ($delete_time = variable_get('inactive_user_auto_delete', 0)) {
-      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', REQUEST_TIME, $delete_time, REQUEST_TIME, $delete_time);
-      while ($user = db_fetch_object($result)) {
-        if ($user->uid && ((variable_get('inactive_user_auto_delete_warn', 0) && db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_delete_timestamp < %d AND protected <> 1', $user->uid, REQUEST_TIME))) || (!variable_get('inactive_user_auto_delete_warn', 0))) && ($user->created < (REQUEST_TIME - $delete_time))) {
+      $result = db_query('SELECT * FROM {users} WHERE ((access <> :zero AND login <> :zero 
+                AND access < (:request_time - :delete_time)) 
+                OR (login = :zero AND created < (:request_time - :delete_time))) AND uid <> :one', array(
+        ':zero' => 0,
+        ':request_time' => REQUEST_TIME, 
+        ':block_time' => $delete_time,
+        ':one' => 1,
+      ));
+      $record = $result->fetchObject();
+        while ($user = $record) {
+          $t = db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND warned_user_delete_timestamp < :request_time AND protected <> :one', array(
+        ':uid' => $user->uid,
+        ':request_time' => REQUEST_TIME, 
+        ':one' => 1,
+      ));
+      $u = $t->fetchObject();
+       if ($user->uid && ((variable_get('inactive_user_auto_delete_warn', 0)) && $u || (!variable_get('inactive_user_auto_delete_warn', 0)) && ($user->created < (REQUEST_TIME - $delete_time)))) {
           if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {
             // this is a protected user, mark as such
-            db_query('UPDATE {inactive_users} SET protected = 1 WHERE uid = %d', $user->uid);
-            if (!db_affected_rows()) {
-              @db_query('INSERT INTO {inactive_users} (uid, protected) VALUES (%d, 1)', $user->uid, $protected);
-            }
-          }
+         $query = db_update('inactive_users');
+          $query->fields(array('protected' => 1, 'uid' => $user->uid));
+          $result = $query->execute();
+          if (!$result) {
+            db_insert('inactive_users')
+            ->fields(array('uid' => $user->uid, 'protected' => $protected))
+            ->execute();
+          } 
+        }
+      }
+    
           else {
             // delete the user
             // not using user_delete() so we can send custom emails and watchdog
@@ -389,7 +523,7 @@ function inactive_user_cron() {
             db_delete('authmap')
               ->condition('uid', $user->uid)
               ->execute();
-            module_invoke_all('user', 'delete', $array, $user);
+            module_invoke_all('user', 'delete', $array, $user); // ??
             if (variable_get('inactive_user_notify_delete', 0)) {
               _inactive_user_mail(t('[@sitename] Account removed', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_delete_notify_text', _inactive_user_mail_text('delete_notify_text')), $delete_time, $user, NULL);
             }
@@ -398,50 +532,23 @@ function inactive_user_cron() {
             }
             watchdog('user', 'user %user deleted due to inactivity', array('%user' => $user->name));
           }
-        }
-      }
-      if ($user_list) {
-        _inactive_user_mail(t('[@sitename] Deleted accounts', array('@sitename' => variable_get('site_name', 'drupal'))), _inactive_user_mail_text('delete_notify_admin_text'), $delete_time, NULL, $user_list);
-        unset($user_list);
+        }   
+        if ($user_list) {
+          _inactive_user_mail(t('[@sitename] Deleted accounts', array('@sitename' => variable_get('site_name', 'drupal'))), _inactive_user_mail_text('delete_notify_admin_text'), $delete_time, NULL, $user_list);
+          unset($user_list);
       }
     }
+    
+    
   }
 }
 
 /**
- * Slighty modified from format_interval() in common.inc to include months.
- */
-function _format_interval($timestamp, $granularity = 2) {
-  $units = array(
-    '1 year|@count years' => 31536000,
-    '1 month|@count months' => 2592000,
-    '1 week|@count weeks' => 604800,
-    '1 day|@count days' => 86400,
-    '1 hour|@count hours' => 3600,
-    '1 min|@count min' => 60,
-    '1 sec|@count sec' => 1,
-  );
-  $output = '';
-  foreach ($units as $key => $value) {
-    $key = explode('|', $key);
-    if ($timestamp >= $value) {
-      $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
-      $timestamp %= $value;
-      $granularity--;
-    }
-
-    if ($granularity == 0) {
-      break;
-    }
-  }
-  return ($output) ? $output : t('0 sec');
-}
-
-/**
  * Get administrator e-mail address(es)
  */
 function _inactive_user_admin_mail() {
-  $admin_mail = db_result(db_query('SELECT mail FROM {users} WHERE uid = 1'));
+  $uid = 1;
+  $admin_mail = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(':uid' => $uid));
   return variable_get('inactive_user_admin_email', variable_get('site_mail', $admin_mail));
 }
 
@@ -461,7 +568,7 @@ function _inactive_user_mail($subject, $message, $period, $user = NULL, $user_li
   if ($user_list) {
     $to = _inactive_user_admin_mail();
     $variables = array(
-      '%period' => _format_interval($period),
+      '%period' => format_interval($period),
       '%sitename' => variable_get('site_name', 'drupal'),
       '%siteurl' => $base_url,
       "%userlist" => $user_list,
@@ -473,7 +580,7 @@ function _inactive_user_mail($subject, $message, $period, $user = NULL, $user_li
       '%username' => $user->name,
       '%useremail' => $user->mail,
       '%lastaccess' => empty($user->access) ? t('never') : format_date($user->access, 'custom', 'M d, Y'),
-      '%period' => _format_interval($period),
+      '%period' => format_interval($period),
       '%sitename' => variable_get('site_name', 'drupal'),
       '%siteurl' => $base_url,
     );
@@ -539,6 +646,10 @@ function _inactive_user_mail_text($message) {
  * users from deletion.
  */
 function _inactive_user_with_content($uid) {
-  return (db_fetch_object(db_query('SELECT uid FROM {node} WHERE uid = :uid', array(':uid' => $uid))) || db_fetch_object(db_query('SELECT uid FROM {comments} WHERE uid = :uid', array(':uid' => $uid))));
+  $w = db_query('SELECT uid FROM {node} WHERE uid = :uid', array(':uid' => $uid));
+  $x = db_query('SELECT uid FROM {comments} WHERE uid = :uid', array(':uid' => $uid));
+    $y = $w->fetchObject();
+    $z = $x->fetchObject();
+    return $y || $z;
 }
 
