diff --git realname.install realname.install
index 586759d..3977961 100644
--- realname.install
+++ realname.install
@@ -53,7 +53,14 @@
  * Implementation of hook_enable().
  */
 function realname_enable() {
-  drupal_set_message(t('The RealName module has been enabled. You may wish to proceed to <a href="!url">the settings page</a>.', array('!url' => url('admin/user/realname'))));  
+  // Clean up orphaned entries from users removed while module disabled
+  db_query("DELETE rn.* FROM {realname} rn WHERE NOT EXISTS (SELECT * FROM {users} u WHERE u.uid=rn.uid)");
+
+  // Create entries in the realname table for any users added while disabled.
+  // Values will be generated and stored on first use
+  db_query('INSERT INTO {realname}(uid) SELECT uid FROM {user}');
+
+  drupal_set_message(t('The RealName module has been enabled. You may wish to proceed to <a href="!url">the settings page</a>.', array('!url' => url('admin/user/realname'))));
 }
 
 /**
@@ -167,4 +174,14 @@
   }
 
   return $ret;
+}
+/**
+ * Implementation of hook_update_N().
+ *
+ * Make sure every user has an entry in the {realname} table.
+ */
+function realname_update_6103() {
+  // Create entries in the realname table for all users
+  // Values will be generated and stored on first use
+  db_query('INSERT INTO {realname}(uid) SELECT uid FROM {user}');
 }
diff --git realname.module realname.module
index 4a90ae0..ab8695e 100644
--- realname.module
+++ realname.module
@@ -464,10 +464,8 @@
 
     case 'insert':
     case 'after_update':
-      $account->realname = _realname_make_name($account);
-      // If we delete it first, then drupal_write_record is always an insert.
-      db_query("DELETE FROM {realname} WHERE uid = %d", $account->uid);
-      drupal_write_record('realname', $account);
+      // force regenerating the realname, and use the new value
+      $account->realname = realname_make_name($account, TRUE);
   }
 }
 
@@ -482,13 +480,13 @@
   }
 
   // NickSI: Yes, we will have another switch later but I would like to separate table update with other processing
-  // Just delete the old nickname from the table. New one is automatically generated on next request.
+  // Update the realname for the author of a content_profile node when it is edited.
   if ( variable_get('realname_profile_module', NULL) == 'content_profile' && module_exists('content_profile') && is_content_profile($node->type)) {
     switch ($op) {
       case 'update':
       case 'insert':
       case 'delete':
-        db_query('DELETE FROM {realname} WHERE uid = %d', $node->uid);
+        realname_make_name($node, TRUE);
     }
   }
 
@@ -767,11 +765,12 @@
  *
  * @param
  *   $account - the user object to update.
+ *   $reset - force generating the realname
  *
  * @return
- *   The retreived "real name" string.
+ *   The retrieved "real name" string.
  */
-function realname_make_name(&$account) {
+function realname_make_name(&$account, $reset = FALSE) {
   $users = &drupal_static('users',array());
   $links = &drupal_static('links',array());
 
@@ -779,23 +778,35 @@
   if ($account->uid == 0) {
     return $account->name;
   }
+
+  if (!$reset){
+    // Check the static cache.
+    if (isset($users[$account->uid])) {
+      $account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
+      return $users[$account->uid];
+    }
+
+    // Check the DB cache.
+    $result = db_result(db_query("SELECT realname FROM {realname} WHERE uid=%d", $account->uid));
+    if (!empty($result)){
+      $users[$account->uid] = $result;
 
-  // Check static cache first.
-  if (isset($users[$account->uid])) {
-    $account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
-    return $users[$account->uid];
+      return $result;
+    }
   }
 
+  // Make realname.
   $result = _realname_make_name($account);
+
+  // If empty, set to username.
   if (empty($result)) {
-    return $account->name;
-  }
-  if (!db_result(db_query("SELECT uid FROM {realname} WHERE uid = %d", $account->uid))) {
-    db_query("INSERT INTO {realname} (uid, realname) VALUES (%d, '%s')", $account->uid, $result);
-  }
-  else {
-    db_query("UPDATE {realname} SET realname = '%s' WHERE uid = %d", $result, $account->uid);
+    $result = $account->name;
   }
+
+  // update static and DB cache
+  $users[$account->uid] = $result;
+  db_query("UPDATE {realname} SET realname = '%s' WHERE uid = %d", $result, $account->uid);
+
   return $result;
 }
 
