diff --git a/user_badges.install b/user_badges.install
index e9799fd..21c9a87 100644
--- a/user_badges.install
+++ b/user_badges.install
@@ -117,6 +117,12 @@ function user_badges_schema() {
   $schema['user_badges_user'] = array(
     'description' => 'Stores which users have which badges.',
     'fields' => array(
+      'ubid' => array(
+        'description' => t('User Badge ID'),
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        ),
       'uid' => array(
         'description' => t('User ID'),
         'type' => 'int',
@@ -141,8 +147,15 @@ function user_badges_schema() {
         'type' => 'int',
         'not null' => FALSE,
         ),
+      'earned' => array(
+        'description' => t('Timestamp when badge was earned.'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        ),
       ),
-    'primary key' => array('bid', 'uid', 'type'),
+    'primary key' => array('ubid'),
     'indexes' => array(
       'uid_bid' => array('uid', 'bid'),
       'uid_weight' => array('uid', 'userweight'),
@@ -269,3 +282,18 @@ function user_badges_update_6104() {
   return $ret;
 }
 
+/**
+ * Implements hook_update_N().
+ * Adds badge timestamp
+ */
+function user_badges_update_6105() {
+  $ret = array();
+  // 'earned' field
+  db_add_field($ret, 'user_badges_user', 'earned', array('description' => 'Timestamp when badge was earned.','type' => 'int','unsigned' => TRUE,'not null' => TRUE,'default' => 0));
+  db_add_index($ret, 'user_badges_user', 'earned', array('earned'));
+
+  // 'ubid' primary key for multiple of the same badge per user
+  db_drop_primary_key($ret, 'user_badges_user');
+  db_add_field($ret, 'user_badges_user', 'ubid', array('type' => 'serial','unsigned' => TRUE,'not null' => TRUE), array('primary key' => array('ubid')));
+  return $ret;
+}
diff --git a/user_badges.module b/user_badges.module
index 7ac8134..1556d00 100644
--- a/user_badges.module
+++ b/user_badges.module
@@ -186,66 +186,37 @@ function user_badges_menu() {
  * When a role is assigned or removed, appropriate badges are added or removed.
  */
 function user_badges_user($op, &$edit, &$account, $category = 'account') {
-  global $user;
   static $badges = array();
-
+  
   switch ($op) {
     case 'load':
       // Have we loaded this user before?
       if (isset($badges[$account->uid])) {
-        $account->badges = $badges[$account->uid];
-        break;
+        $account->badges_all = $badges[$account->uid];
       }
-
-      $account->badges = array();
-
-      if ($account->uid > 0) {
-        // Get all user badges for this user, regardless of whether we filter the ones we show.
-        $account->badges_all = user_badges_get_badges($account->uid, array('nolimit' => TRUE));
-
-        // Now make the array of badges we will show.
-        $account->badges = $account->badges_all;
-        if ($limit = variable_get('user_badges_showone', 0)) {
-//  dsm("Limit: $limit, account->badges: ".print_r($account->badges, true));
-          // Loop through all potential badges and get the ones we can show.
-//          if ($account->badges_all) {
-            foreach ($account->badges_all as $bid => $badge) {
-              $badge->class = 'badge ' . _user_badges_class($badge);
-              // Display the badge if there's no limit or if the badge is unhideable or if we are within our limit.
-              if ($limit > 0 || $badge->unhideable == 1) {
-                $account->badges[$bid] = $badge;
-                // Count down our limit, unless the badge doesn't count towards it.
-                if (!$badge->doesnotcounttolimit) {
-                  $limit--;
-                }
-              }
-            }
-//          }
-        }
-//        else {
-//          $account->badges = $account->badges_all;
-//        }
+      elseif ($account->uid >= 1) {
+        $account->badges_all = user_badges_get_user_badges($account->uid, array('nolimit' => TRUE, 'refresh' => TRUE));
+        $badges[$account->uid] = $account->badges_all;
       }
 
-      $badges[$account->uid] = $account->badges;
+      // Create limited array of badges
+      $account->badges = (count($account->badges_all)) ? _user_badges_limit($account->badges_all) : array();
+
       break;
 
     case 'insert':
       if (is_array($account->roles)) {
-        // Get the list of role badges.
         $roles = user_badges_get_roles();
-        $badges = user_badges_get_badges('select');
-        $message = user_access('manage badges');
+        
         $rids = array_keys($account->roles);
         foreach ($rids as $rid) {
           // If this role has a badge...
-          if (key_exists($rid, $roles)) {
-            // and user doesn't already have this badge.
-            if (!key_exists($roles[$rid], $account->badges)) {
-              $success = user_badges_user_add_badge($account->uid, $roles[$rid], 'role');
-              if ($success && $message) {
-                drupal_set_message(t('User assigned %name badge.', array('%name' => $badges[$roles[$rid]])));
-              }
+          if (key_exists($rid, $roles) && !_user_badges_bid_in_badges($roles[$rid], $account->badges)) {
+            $success = user_badges_user_add_badge($account->uid, $roles[$rid], 'role');
+            $can_manage = user_access('manage badges');
+            if ($success && $can_manage) {
+              $badges = user_badges_get_badges('select');
+              drupal_set_message(t('User assigned %name badge.', array('%name' => $badges[$roles[$rid]])));
             }
           }
         }
@@ -254,24 +225,21 @@ function user_badges_user($op, &$edit, &$account, $category = 'account') {
 
     case 'update':
       if (is_array($edit['roles'])) {
-        // Badges only get assigned or removed when a user's role assignments are changed.
-
         // Add authenticated users (code below only cares about array keys) to prevent badge deletion
         $new_roles = $edit['roles'];
         $new_roles[2] = 2;
         // Get the list of role badges.
         $roles = user_badges_get_roles();
-        $badges = user_badges_get_badges('select');
-
-        $message = user_access('manage badges');
 
         // What are the added roles?
         $added = array_diff(array_keys($new_roles), array_keys((array)$account->roles));
         foreach ($added as $rid) {
           // if this role has a badge
-          if (key_exists($rid, $roles) && !key_exists($roles[$rid], $account->badges_all)) {
+          if (key_exists($rid, $roles) && !_user_badges_bid_in_badges($roles[$rid], $account->badges_all)) {
             $success = user_badges_user_add_badge($account->uid, $roles[$rid], 'role');
-            if ($success && $message) {
+            $can_manage = user_access('manage badges');
+            if ($success && $can_manage) {
+              $badges = user_badges_get_badges('all');
               drupal_set_message(t('User assigned %name badge.', array('%name' => $badges[$roles[$rid]])));
             }
           }
@@ -281,15 +249,15 @@ function user_badges_user($op, &$edit, &$account, $category = 'account') {
         $removed = array_diff(array_keys((array)$account->roles), array_keys($new_roles));
         foreach ($removed as $rid) {
           // If this role has a badge and user has this badge..
-          if (key_exists($rid, $roles) && key_exists($roles[$rid], $account->badges_all)) {
-            $success = user_badges_user_remove_badge($account->uid, $roles[$rid], 'role');
+          if (key_exists($rid, $roles) && _user_badges_bid_in_badges($roles[$rid], $account->badges_all)) {
+            user_badges_user_remove_badge($account->uid, $roles[$rid], 'role');
             drupal_set_message(t('%name badge removed from user.', array('%name' => $badges[$roles[$rid]])));
           }
         }
 
         //As we may have altered the badges, we need to refresh them in the $account object
-        $account->badges = user_badges_get_badges($account->uid);
-        $account->badges_all = user_badges_get_badges($account->uid, array('nolimit' => TRUE));
+        $account->badges_all = user_badges_get_user_badges($account->uid, array('nolimit' => TRUE, 'refresh' => TRUE));
+        $account->badges = (count($account->badges_all)) ? _user_badges_limit($account->badges_all) : array();
       }
       break;
 
@@ -299,10 +267,11 @@ function user_badges_user($op, &$edit, &$account, $category = 'account') {
 
     case 'view':
       if (is_array($account->badges) && count($account->badges)) {
-        $badgeimgs = array();
+        $badge_images = array();
         foreach ($account->badges as $badge) {
-          $badgeimgs[] = theme('user_badge', $badge, $account);
+          $badge_images[] = theme('user_badge', $badge, $account);
         }
+
         $account->content['user_badges'] = array(
           '#type' => 'user_profile_category',
           '#title' => t('Badges'),
@@ -311,7 +280,7 @@ function user_badges_user($op, &$edit, &$account, $category = 'account') {
         );
         $account->content['user_badges']['badges'] = array(
           '#type' => 'user_profile_item',
-          '#value' => theme('user_badge_group', $badgeimgs),
+          '#value' => theme('user_badge_group', $badge_images),
           '#attributes' => array('class' => 'badges'),
         );
       }
@@ -319,25 +288,6 @@ function user_badges_user($op, &$edit, &$account, $category = 'account') {
 }
 
 /**
- * Helper function for building badge class names.
- * I was originally using form_clean_id, but it is not secure.
- *
- * @param $badge - the object describing the badge.
- * @return string containing the class name.
- */
-function _user_badges_class($badge) {
-  // Doing separate lines makes changing the algorithm easier.
-  $class = $badge->name;
-  $class = strip_tags($class, '');
-  $class = drupal_strtolower($class);
-  $class = str_replace(array('"', "'"), '', $class);
-  $class = str_replace(array('_', ' '), '-', $class);
-
-//  dsm("_user_badges_class: badge->name = $badge->name becomes $class.");
-  return $class;
-}
-
-/**
  * Implements hook_theme().
  */
 function user_badges_theme() {
@@ -391,10 +341,10 @@ function user_badges_userweight_form($form_state, $account) {
       $weight = $badge->userweight;
     }
 
-    $form['name'][$badge->bid] = array('#value' => check_plain($badge->name));
-    $form['badge'][$badge->bid] = array('#value' => theme('user_badge', $badge, $account));
+    $form['name'][$badge->ubid] = array('#value' => check_plain($badge->name));
+    $form['badge'][$badge->ubid] = array('#value' => theme('user_badge', $badge, $account));
 
-    $form['weight'][$badge->bid] = array(
+    $form['weight'][$badge->ubid] = array(
       '#type' => 'weight',
       '#default_value' => $weight,
       '#delta' => $delta,
@@ -420,10 +370,10 @@ function user_badges_userweight_form($form_state, $account) {
  */
 function user_badges_userweight_form_submit($form, &$form_state) {
   if (isset($form['weight']) && is_array($form['weight'])) {
-    foreach (element_children($form['weight']) as $bid) {
-      db_query("UPDATE {user_badges_user} SET userweight = %d WHERE bid = %d AND uid = %d",
-      $form_state['values'][$bid],
-      $bid,
+    foreach (element_children($form['weight']) as $ubid) {
+      db_query("UPDATE {user_badges_user} SET userweight = %d WHERE ubid = %d AND uid = %d",
+      $form_state['values'][$ubid],
+      $ubid,
       $form_state['values']['uid']
       );
     }
@@ -532,7 +482,6 @@ function user_badges_badge_autocomplete_validation($value) {
   }
 }
 
-
 /**
  * Define the page on user/uid/badges/edit.
  */
@@ -559,10 +508,7 @@ function user_badges_userweight_page($uid) {
     return drupal_get_form('user_badges_userweight_form', $account);
   }
   else {
-    // Otherwise, just list the badges on the page.
-    $user_badges = user_badges_get_badges($account->uid, array('nolimit' => TRUE));
-    $badges = array();
-    foreach ((array)$user_badges as $badge) {
+    foreach ($account->badges_all as $badge) {
       $badges[] = theme('user_badge', $badge, $account);
     }
     if ($badges) {
@@ -611,7 +557,7 @@ function user_badges_change_form(&$form_state, $account) {
       '#collapsed' => FALSE,
     );
     foreach ($account->badges_all as $badge) {
-      $form['remove'][$badge->bid] = array(
+      $form['remove'][$badge->ubid] = array(
         '#type' => 'checkbox',
         '#title' => theme('user_badge', $badge, $account),
         '#return_value' => 1,
@@ -650,12 +596,11 @@ function user_badges_change_form_validate($form, &$form_state) {
 
 /**
  * Process user_badges_remove_form form submissions.
- *
  * Add the named badge. Remove the checked badges.
  */
 function user_badges_change_form_submit($form, &$form_state) {
   $uid = $form_state['values']['uid'];
-
+  
   //Add badges for non-empty fields
   for ($i = 1; $i <= 5; $i++) {
     if (!empty($form_state['values']['add'. $i])) {
@@ -665,70 +610,20 @@ function user_badges_change_form_submit($form, &$form_state) {
   }
 
   //Remove any checked badges
-  $badges_to_go = array();
-  foreach ($form_state['values'] as $bid => $value) {
-    if (is_numeric($bid) && $value == 1) {
-      $badges_to_go[] = $bid;
+  $badges_removed = 0;
+  foreach ($form_state['values'] as $ubid => $value) {
+    if (is_numeric($ubid) && $value == 1) {
+      $badges_removed = (user_badges_user_remove_badge($ubid)) ? $badges_removed + 1 : $badges_removed;
     }
   }
-  if (count($badges_to_go)) {
-    foreach ($badges_to_go as $bid) {
-      user_badges_user_remove_badge($uid, $bid);
-    }
-    drupal_set_message(t('!removalcount badge(s) removed.', array('!removalcount' => count($badges_to_go))));
+  
+  if ($badges_removed >= 1) {
+    drupal_set_message(t('!removalcount badge(s) removed.', array('!removalcount' => $badges_removed)));
   }
 
 }
 
 /**
- * Assign user badges to a user
- *
- * @param $edit is an array containing badges array
- * @param $uid is the user id
- * @param $quiet suppresses message display
- */
-function user_badges_user_save($edit, $uid, $quiet = TRUE) {
-  $badges = user_badges_get_badges($uid);
-
-  if (is_array($edit)) {
-    // an array of just the checked boxes please
-    $newbadges = array();
-    foreach ($edit as $bid => $is_selected) {
-      if ($is_selected) {
-        $newbadges[] = $bid;
-      }
-    }
-
-    $success = TRUE;
-
-    // what are the added badges?
-    $added = array_diff($newbadges, array_keys($badges));
-
-    foreach ($added as $bid) {
-      if (!key_exists($bid, $badges)) {
-        $success = (boolean) user_badges_user_add_badge($uid, $bid);
-      }
-    }
-
-    // what are the removed badges?
-    $removed = array_diff(array_keys($badges), $newbadges);
-
-    foreach ($removed as $bid) {
-      // and user has this badge
-      if (key_exists($bid, $badges)) {
-        $success = $success && (boolean) user_badges_user_remove_badge($uid, $bid);
-      }
-    }
-    if ($success && !$quiet) {
-      drupal_set_message(t('Badges saved.'));
-    }
-    elseif (!$quiet) {
-      drupal_set_message(t('There was a problem saving badges to the database.'), 'error');
-    }
-  }
-}
-
-/**
  * Add a badge to user.
  *
  * @param $uid User ID.
@@ -738,10 +633,22 @@ function user_badges_user_save($edit, $uid, $quiet = TRUE) {
  * @return bool with query success
  */
 function user_badges_user_add_badge($uid, $bid, $type = NULL) {
-  user_badges_user_remove_badge($uid, $bid, $type);
-  return db_query('INSERT INTO {user_badges_user} (uid, bid, type) VALUES (%d, %d, \'%s\')', $uid, $bid, $type);
-}
+  $add_badge = (object) array(
+    'uid' => $uid,
+    'bid' => $bid,
+    'type' => $type,
+    'earned' => time(),
+    );
+  $success = drupal_write_record('user_badges_user', $add_badge);
+
+  // Integrate rules events.
+  if (module_exists('rules')) {
+    $arguments = array('user' => user_load($uid), 'badge' => user_badges_get_user_badge($add_badge->ubid));
+    rules_invoke_event('user_badges_badge_given', $arguments);
+    }
 
+  return $success;
+}
 
 /**
  * remove a badge from user.
@@ -749,108 +656,98 @@ function user_badges_user_add_badge($uid, $bid, $type = NULL) {
  * @param $uid User ID.
  * @param $bid Badge ID.
  * @param $type Whether set as part of the role, or individually assigned ('user', 'role').
+ * @param $ubid The specific uniquely-earned badge
  *
  * @return bool with query success
  */
-function user_badges_user_remove_badge($uid, $bid, $type = NULL) {
-  if (is_null($type)) {
-    return db_query('DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d', $uid, $bid);
-  }
-  else {
-    return db_query('DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d AND type=\'%s\'', $uid, $bid, $type);
-  }
-}
+function user_badges_user_remove_badge($ubid) {
+  // Rules integration
+  if (module_exists('rules')) {
+    $arguments = array('user' => user_load($uid), 'badge' => user_badges_get_user_badge($ubid));
+    rules_invoke_event('user_badges_badge_removed', $arguments);
+    }
 
+  return db_query('DELETE FROM {user_badges_user} WHERE ubid=%d', $ubid);
+}
 
 /**
  * Return array of user badges where keys are badge ids (bid)
  *   and values are object containing badge info.
- * @param $uid
- *   if $uid is a user id, returns badges for that user.
- *   if $uid is 'all', returns all badges.
- *   if $uid is 'select', returns badges for form_select options.
- * @param $options array of options.
- *   $options['nolimit'] : if TRUE, the limit clause will not be applied for a user
- *   returned values for 'select' are just badge names.
+ * @param $amount
+ *   if $amount is 'all', returns all badges.
+ *   if $amount is 'select', returns badges for form_select options.
  */
-function user_badges_get_badges($uid, $options = array()) {
-  static $badges = array(), $past_uid, $past_options;
-
-  if (isset($badges[$uid])) {
-    return $badges[$uid];
-  }
-
-  // Do this so we don't return NULL.
-  $badges[$uid] = array();
+function user_badges_get_badges($amount = 'all') {
+  if(!isset($all_badges)){
+    static $all_badges = array();
+    $sql = db_query('SELECT b.bid, b.weight, b.name, b.image, b.href,
+      b.unhideable, b.fixedweight, b.doesnotcounttolimit, b.tid
+      FROM {user_badges_badges} b
+      ORDER BY b.weight, b.name');
 
-  if (empty($past_uid) || $past_uid !== $uid || $past_options !== $options ) {
-    $past_uid = $uid;
-    if ($uid == 'all' || $uid == 'select') {
-      $sql = db_query('SELECT b.bid, b.weight, b.name, b.image, b.href,
-        b.unhideable, b.fixedweight, b.doesnotcounttolimit, b.tid
-        FROM {user_badges_badges} b
-        ORDER BY b.weight, b.name');
+    while ($badge = db_fetch_object($sql)) {
+      $all_badges[$badge->bid] = $badge;
+      $all_badges[$badge->bid]->class = 'badge ' . _user_badges_class($badge);
     }
-    else {
-      $usr = db_result(db_query('SELECT COUNT(uid) FROM {users} WHERE uid = %d AND status = 0', $uid));
-
-      if ($usr && variable_get('user_badges_showblocked', 0)) {
-        $sql = db_query('SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
-          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid,
-          CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
-          FROM {user_badges_badges} b
-            INNER JOIN {user_badges_user} u ON b.bid = u.bid
-            INNER JOIN {user_badges_roles} r ON b.bid = r.bid
-          WHERE u.uid = %d AND r.rid = 0
-          ORDER BY coalescedweight, b.name',
-          $uid);
-      }
-      else {
-        $query = 'SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
-          b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.userweight, b.tid,
-          CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
-          FROM {user_badges_badges} b
-            INNER JOIN {user_badges_user} u ON b.bid = u.bid
-          WHERE u.uid = %d
-          ORDER BY coalescedweight, b.name
-          ';
-        $sql = db_query($query, $uid);
+  }
 
-      }
+  // Output the [cached] badges
+  if ($amount == 'select') {
+    $select_badges = array();
+    foreach ($all_badges as $bid => $badge) {
+      $select_badges[$bid] = $badge->name;
+      $select_badges[$bid]['#attributes'] = array('class' => 'badge ' . _user_badges_class($badge));
     }
+    return $select_badges;
+  }
+  else {
+    return $all_badges;
+  }
+}
 
-    // Should we limit the badges returned?
-    if (!$options['nolimit'] && variable_get('user_badges_showone', 0)) {
-      $limit = variable_get('user_badges_showone', 1);
+/**
+ * Return array of user badges where keys are user badge ids (ubid)
+ * and values are objects containing badge info.
+ * @param $uid a user id
+ * @param $options array of options
+ *   $options['nolimit'] : if TRUE, the limit clause will not be applied for a user_error
+ *   $options['refresh'] : if TRUE, will poll the database regardless of if there's a cached version
+ */
+function user_badges_get_user_badges($uid, $options = array('nolimit' => FALSE, 'refresh' => FALSE)){
+  static $badge_cache = array();
+
+  if (!isset($badge_cache[$uid]) || $options['refresh']) {
+    if (variable_get('user_badges_showblocked', 0)) {
+      $sql = db_query('SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
+        b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.ubid, u.userweight, u.earned, b.tid,
+        CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
+        FROM {user_badges_badges} b
+        INNER JOIN {user_badges_user} u ON b.bid = u.bid
+        INNER JOIN {user_badges_roles} r ON b.bid = r.bid
+        WHERE u.uid = %d AND r.rid = 0
+        ORDER BY coalescedweight, b.name',
+        $uid);
     }
     else {
-      // Set to -1 for no limit.
-      $limit = -1;
+      $sql = db_query('SELECT DISTINCT b.bid, b.weight, b.name, b.image, b.href,
+        b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.ubid, u.userweight, u.earned, b.tid,
+        CASE WHEN b.fixedweight = 1 THEN b.weight ELSE COALESCE(u.userweight,b.weight) END coalescedweight
+        FROM {user_badges_badges} b
+        INNER JOIN {user_badges_user} u ON b.bid = u.bid
+        WHERE u.uid = %d
+        ORDER BY coalescedweight, b.name', 
+        $uid);
     }
 
     while ($badge = db_fetch_object($sql)) {
-      // Display the badge if there's no limit or if the badge is unhideable or if we are within our limit.
-      if ($limit != 0 || $badge->unhideable == 1) {
-        if ($uid == 'select') {
-          $badges[$uid][$badge->bid] = $badge->name;
-          $badges[$badge->bid]['#attributes'] = array('class' => 'badge ' . _user_badges_class($badge));
-        }
-        else {
-          $badges[$uid][$badge->bid] = $badge;
-          $badges[$uid][$badge->bid]->class = 'badge ' . _user_badges_class($badge);
-        }
-        //Count down our limit, unless the badge doesn't count towards it
-        if (!$badge->doesnotcounttolimit) {
-          $limit--;
-        }
-      }
+      $badge_cache[$uid][$badge->ubid] = $badge;
+      $badge_cache[$uid][$badge->ubid]->class = 'badge ' . _user_badges_class($badge);
     }
   }
 
-  return $badges[$uid];
+  return ($options['nolimit']) ? $badge_cache[$uid] : _user_badges_limit($badge_cache[$uid]);
 }
 
-
 /**
  * Return badge object for given badge id
  */
@@ -858,7 +755,18 @@ function user_badges_get_badge($bid) {
   return db_fetch_object(db_query('SELECT * FROM {user_badges_badges} WHERE bid = %d', $bid));
 }
 
-
+/**
+ * Return user badge object for given user badge id
+ * @param $ubid the user badge id
+ */
+function user_badges_get_user_badge($ubid) {
+  return db_fetch_object(db_query('SELECT b.bid, b.weight, b.name, b.image, b.href,
+      b.unhideable, b.fixedweight, b.doesnotcounttolimit, u.ubid, u.userweight, u.earned, b.tid
+      FROM {user_badges_badges} b
+      INNER JOIN {user_badges_user} u ON b.bid = u.bid
+      WHERE u.ubid = %d',
+      $ubid));
+}
 
 function user_badges_delete_form($form_state, $bid) {
   if ($badge = user_badges_get_badge($bid)) {
@@ -879,7 +787,6 @@ function user_badges_delete_form_submit($form, &$form_state) {
   $form_state['redirect'] = 'admin/user/user_badges';
 }
 
-
 /**
  * Returns an array where keys are role ids (rid) and values are the badge ids (bid) associated with that role
  * These values are assigned on admin/user/user_badges/roles
@@ -899,11 +806,11 @@ function user_badges_get_roles($rid = NULL, $options = array()) {
     $sql = db_query('SELECT ubr.rid, ubr.bid, ubb.name, ubb.image, ubb.weight, ubb.href, ubb.tid FROM {user_badges_roles} ubr INNER JOIN {user_badges_badges} ubb ON ubb.bid=ubr.bid WHERE ubr.rid = %d', $rid);
   }
   else {
-    $sql = db_query('SELECT ubr.rid, ubr.bid, ubb.name, ubb.image, ubb.weight, ubb.href, ubb.tid FROM {user_badges_roles} ubr INNER JOIN {user_badges_badges} ubb ON ubb.bid=ubr.bid', $rid);
+    $sql = db_query('SELECT ubr.rid, ubr.bid, ubb.name, ubb.image, ubb.weight, ubb.href, ubb.tid FROM {user_badges_roles} ubr INNER JOIN {user_badges_badges} ubb ON ubb.bid=ubr.bid');
   }
   while ($row = db_fetch_object($sql)) {
     if ($options['returnbadges']) {
-      $row->image = _user_badges_build_image($row);
+      $row->image = user_badges_build_image($row);
       $roles[$row->rid] = $row;
     }
     else {
@@ -941,38 +848,39 @@ function user_badges_save_roles($roles) {
 
         // The blocked user "role" (represented as rid 0) has no entry in
         // the users_role table, so it needs its own special query.
+        $earned = time();
         if ($rid == 0) {
           $success = $success && db_query("
-            INSERT INTO {user_badges_user} (uid, bid, type)
-            SELECT u.uid, %d, 'role'
+            INSERT INTO {user_badges_user} (uid, bid, type, earned)
+            SELECT u.uid, %d, 'role', %d
             FROM {users} u
             LEFT JOIN {user_badges_user} ubu
-            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role'
+            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role' AND ubu.earned=%d
             WHERE status = 0 AND ubu.uid IS NULL
-          ", $bid, $bid);
+          ", $bid, $earned, $bid, $earned);
         }
         // The authenticated user role (represented as rid 2) has no entry
         // in the users_role table, so it needs its own special query.
         elseif ($rid == 2) {
           $success = $success && db_query("
-            INSERT INTO {user_badges_user} (uid, bid, type)
-            SELECT u.uid, %d, 'role'
+            INSERT INTO {user_badges_user} (uid, bid, type, earned)
+            SELECT u.uid, %d, 'role', %d
             FROM {users} u
             LEFT JOIN {user_badges_user} ubu
-            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role'
+            ON ubu.uid=u.uid AND ubu.bid=%d AND ubu.type='role' AND ubu.earned=%d
             WHERE u.uid > 0 AND ubu.uid IS NULL
-          ", $bid, $bid);
+          ", $bid, $earned, $bid, $earned);
         }
         // For all the normal roles, we want to run this query.
         else {
           $success = $success && db_query("
-            INSERT INTO {user_badges_user} (uid, bid, type)
-            SELECT ur.uid, %d, 'role'
+            INSERT INTO {user_badges_user} (uid, bid, type, earned)
+            SELECT ur.uid, %d, 'role', %d
             FROM {users_roles} ur
             LEFT JOIN {user_badges_user} ubu
-            ON ubu.uid=ur.uid AND ubu.bid=%d AND ubu.type='role'
+            ON ubu.uid=ur.uid AND ubu.bid=%d AND ubu.type='role' AND ubu.earned=%d
             WHERE ur.rid=%d AND ubu.uid IS NULL
-          ", $bid, $bid, $rid);
+          ", $bid, $earned, $bid, $earned, $rid);
         }
       }
     }
@@ -993,54 +901,17 @@ function user_badges_save_roles($roles) {
  *   @return string html representation of userbadges
  */
 function user_badges_for_uid($uid, $refresh = FALSE) {
-  static $cache;
-  if ($uid) {
-    if (isset($cache[$uid]) && !$refresh) {
-      return $cache[$uid];
-    }
-    else {
-      $user_badges = user_badges_get_badges($uid);
-        foreach ((array)$user_badges as $badge) {
-        $badges[] = theme('user_badge', $badge, user_load($uid));
-      }
-      $cache[$uid] = isset($badges) ? theme('user_badge_group', $badges) : '';
-      return $cache[$uid];
+  static $html_cache;
+  if ($uid && (!isset($html_cache[$uid]) || $refresh)) {
+    $badge_user = user_load($uid);
+    $user_badges = $badge_user->badges;
+    foreach ($user_badges as $badge) {
+      $badges[] = theme('user_badge', $badge, $badge_user);
     }
-  }
-}
-
-/**
- * Get all user badges for a user.
- * @param $load is array defining criteria for user_load().
- *   The most common use will be:
- *   user_badges_for_user(array('uid'=>123));
- * @return string containing HTML representation of user badges for given user.
- */
-function user_badges_for_user($load, $list = FALSE) {
-  // @TODO: cache the results to minimize queries.
-  $account = user_load($load);
-
-  foreach ((array)$account->badges as $badge) {
-    $badges[] = theme('user_badge', $badge, $account);
+    $html_cache[$uid] = isset($badges) ? theme('user_badge_group', $badges) : '';
   }
 
-  if ($list) {
-    $badges = array(theme('item_list', $badges));
-  }
-
-  if ($badges) {
-    return theme('user_badge_group', $badges);
-  }
-  else {
-    // Do we have a "no badges" message?
-    if ($nobadges = variable_get('user_badges_nobadges', '')) {
-      $nobadges = '<div class="user_badges_no_badges">' . filter_xss_admin(t($nobadges)) . '</div>';
-      return theme('user_badge_group', array($nobadges));
-    }
-    else {
-      return FALSE;
-    }
-  }
+  return ($uid) ? $html_cache[$uid] : '';
 }
 
 /**
@@ -1056,7 +927,7 @@ function theme_user_badge_group($badgeimages) {
 /**
  * Helper function to produce badge image.
  */
-function _user_badges_build_image($badge) {
+function user_badges_build_image($badge) {
   if (!isset($badge->class)) {
     $badge->class = 'badge ' . _user_badges_class($badge);
   }
@@ -1080,8 +951,8 @@ function _user_badges_build_image($badge) {
  */
 function theme_user_badge($badge, $account = NULL) {
   //If we haven't been supplied with a user, use whoever is logged in
-  global $user;
   if (is_null($account)) {
+    global $user;
     $account = $user;
   }
 
@@ -1089,12 +960,11 @@ function theme_user_badge($badge, $account = NULL) {
     $badge->class = 'badge ' . _user_badges_class($badge);
   }
 
-  $image = _user_badges_build_image($badge);
+  $image = user_badges_build_image($badge);
 
   // We don't link the badge if there is no link and no default,
   // or if the default is overridden.
-  if (($badge->href == "" && !variable_get('user_badges_defaulthref', ''))
-    || drupal_strtolower($badge->href) == '<none>') {
+  if (($badge->href == "" && !variable_get('user_badges_defaulthref', '')) || drupal_strtolower($badge->href) == '<none>') {
     return $image;
   }
   else {
@@ -1146,7 +1016,7 @@ function user_badges_action_info() {
 }
 
 /**
- * Implementsa Drupal action.
+ * Implements a Drupal action.
  * Adds a badge to the current user.
  */
 function user_badges_add_badge_action(&$account, $context = array()) {
@@ -1204,7 +1074,7 @@ function user_badges_add_badge_action_submit($form, $form_state) {
 }
 
 /**
- * Implementsa Drupal action.
+ * Implements a Drupal action.
  * Removes a badge to the current user.
  */
 function user_badges_remove_badge_action(&$account, $context = array()) {
@@ -1219,15 +1089,15 @@ function user_badges_remove_badge_action(&$account, $context = array()) {
     $uid = $user->uid;
   }
   $success = TRUE;
-  $badges = user_badges_get_badges('all');
+  $badges = user_badges_get_user_badges($uid, array('nolimit' => TRUE));
 
-  foreach ($context['badges'] as $bid) {
-    $success = (boolean) user_badges_user_remove_badge($uid, $bid, 'uid');
+  foreach ($context['badges'] as $ubid) {
+    $success = (boolean) user_badges_user_remove_badge($ubid);
     if ($success) {
-      watchdog('action', 'Removed user badge %badge to user %name.', array('%name' => check_plain($user->name), '%badge' => check_plain($badges[$bid]->name)));
+      watchdog('action', 'Removed user badge %badge to user %name.', array('%name' => check_plain($user->name), '%badge' => check_plain($badges[$ubid]->name)));
     }
     else {
-      watchdog('action', 'Unable to remove user badge %badge to user %name.', array('%name' => check_plain($user->name), '%badge' => check_plain($badges[$bid]->name)), WATCHDOG_WARNING);
+      watchdog('action', 'Unable to remove user badge %badge to user %name.', array('%name' => check_plain($user->name), '%badge' => check_plain($badges[$ubid]->name)), WATCHDOG_WARNING);
     }
   }
 }
@@ -1239,11 +1109,11 @@ function user_badges_remove_badge_action_form($context) {
   $context['badges'] = is_array($context['badges']) ? $context['badges'] : array();
 
   foreach ($badges as $badge) {
-    $form['badges'][$badge->bid] = array(
+    $form['badges'][$badge->ubid] = array(
       '#type' => 'checkbox',
       '#title' => theme('user_badge', $badge),
       '#return_value' => 1,
-      '#default_value' => $context['badges'][$badge->bid],
+      '#default_value' => $context['badges'][$badge->ubid],
       '#description' => check_plain($badge->name),
     );
   }
@@ -1263,14 +1133,18 @@ function user_badges_remove_badge_action_submit($form, $form_state) {
 
 /**
  * Implements hook_token_values().
- * @TODO: Add tokens for Submitted By module?
  */
 function user_badges_token_values($type, $object = NULL) {
-  if ($type == 'userbadge') {
-    $badge = $object;
-    $tokens['userbadge-name'] = $badge->name;
-    $tokens['userbadge-bid'] = $badge->bid;
-    return $tokens;
+  if ($type == 'userbadge' && !is_null($object)) {
+    return array(
+      'bid' => $object->bid,
+      'name' => $object->name,
+      'image' => $object->image,
+      'href' => $object->href,
+      'ubid' => $object->ubid,
+      'earned' => $object->earned,
+      'tid' => $object->tid
+      );
   }
 }
 
@@ -1279,8 +1153,14 @@ function user_badges_token_values($type, $object = NULL) {
  */
 function user_badges_token_list($type = 'all') {
   if ($type == 'userbadge' || $type == 'all') {
-    $tokens['userbadge']['userbadge-name'] = t("The badge name");
-    $tokens['userbadge']['userbadge-bid'] = t("The badge id number (bid)");
+    $tokens = array();
+    $tokens['userbadge']['bid'] = t("The badge id number");
+    $tokens['userbadge']['name'] = t("The badge name");
+    $tokens['userbadge']['image'] = t("The path to the badge image");
+    $tokens['userbadge']['href'] = t("The path to the badge info page");
+    $tokens['userbadge']['ubid'] = t("The user badge id number");
+    $tokens['userbadge']['earned'] = t("Timestamp when the badge was earned");
+    $tokens['userbadge']['tid'] = t("The taxonomy term associated with the badge");
     return $tokens;
   }
 }
@@ -1291,13 +1171,10 @@ function user_badges_block($op = 'list', $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
       return user_badges_block_list();
-
     case 'view':
       return user_badges_block_view($delta);
-
     case 'configure':
       return user_badges_block_configure($delta);
-
     case 'save':
       return user_badges_block_save($delta, $edit);
   }
@@ -1324,7 +1201,7 @@ function user_badges_block_view($delta = 0) {
       if (arg(0) == 'node' && is_numeric(arg(1)) && empty($arg2)) {
         $node = menu_get_object();
         if (in_array($node->type, variable_get('user_badges_current_node_types', array()))) {
-          $badges = user_badges_for_user(array('uid' => $node->uid));
+          $badges = user_badges_for_uid($node->uid);
           $account = user_load(array('uid' => $node->uid));
           $block['title'] = t("@name's Badges", array('@name' => $account->name));
           $block['content'] = $badges;
@@ -1369,18 +1246,62 @@ function user_badges_block_save($delta = 0, $edit = array()) {
 }
 
 /**
- * Implements hook_cron()
- * UNCOMMENT in order to get User Badges to work along with Auto Assign Role
- * notes: - this is a quite fast solution in order to solve Auto Assign Role issue
- *        - do not forget to set up your cron
+ * Helper function for building badge class names.
+ * I was originally using form_clean_id, but it is not secure.
+ *
+ * @param $badge - the object describing the badge.
+ * @return string containing the class name.
+ */
+function _user_badges_class($badge) {
+  // Doing separate lines makes changing the algorithm easier.
+  $class = $badge->name;
+  $class = strip_tags($class, '');
+  $class = drupal_strtolower($class);
+  $class = str_replace(array('"', "'"), '', $class);
+  $class = str_replace(array('_', ' '), '-', $class);
+
+//  dsm("_user_badges_class: badge->name = $badge->name becomes $class.");
+  return $class;
+}
+
+/**
+ * Helper function for checking if a user has a badge
+ *
+ * @param $bid the badge ID to check against
+ * @param $badges the user's badges
+ * @return (bool) TRUE if the bid is among the badges
+ */
+function _user_badges_bid_in_badges($bid, $badges) {
+  $bids = array();
+  foreach($badges as $badge) {
+    $bids[] = $badge->bid;
+  }
+  return in_array($bid, $bids);
+}
+
+/**
+ * Helper function to limit an array of badges
+ * Only checks the limit, not the user, so user validation is required where this is used
+ * @param $badges an array of badges
+ * @return the array, reduced by the limit option
  */
-// function user_badges_cron() {
-//   $result = db_query('SELECT * FROM {user_badges_roles}');
-//
-//   $roles = array();
-//   while ($o = db_fetch_object($result)) {
-//     $roles[$o->rid] = $o->bid;
-//   }
-//
-//   user_badges_save_roles($roles);
-// }
+function _user_badges_limit($badges) {
+  $return_badges = array();
+  $limit = variable_get('user_badges_showone', 0);
+  
+  if ($limit) {
+    foreach($badges as $ubid => $badge) {
+      if ($limit >= 1 || $badge->unhideable == 1) {
+        $return_badges[$ubid] = $badge;
+        if (!$badge->doesnotcounttolimit) {
+          $limit--;
+        }
+      }
+    }
+  }
+  else {
+    $return_badges = $badges;
+  }
+
+  return $return_badges;
+}
\ No newline at end of file
diff --git a/user_badges.rules.inc b/user_badges.rules.inc
new file mode 100644
index 0000000..f7a44bf
--- /dev/null
+++ b/user_badges.rules.inc
@@ -0,0 +1,310 @@
+<?php
+/**
+* @file
+* Provide integration with Rules module
+* TODO: Add a condition for when the badge was earned, which has potential usefulness for the badge removed event
+*/
+
+/**
+* Implementation of hook_rules_data_type_info().
+*/
+function user_badges_rules_data_type_info() {
+  return array(
+    'userbadge' => array(
+      'label' => t('User Badge'),
+      'class' => 'rules_data_type_userbadge',
+      'savable' => FALSE,
+      'identifiable' => TRUE,
+      'uses_input_form' => FALSE,
+      'module' => 'User Badges',
+    ),
+  );
+}
+
+/**
+* Defines the rules userbadge data type.
+*/
+class rules_data_type_userbadge extends rules_data_type {
+  function load($ubid) {
+    return user_badges_get_user_badge($ubid);
+  }
+
+  function get_identifier() {
+    $badge = &$this->get();
+    return $badge->ubid;
+  }
+}
+
+/**
+* Implementation of hook_rules_event_info().
+*/
+function user_badges_rules_event_info() {
+ return array(
+   'user_badges_badge_given' => array(
+     'label' => t('User was given a badge'),
+     'arguments' => array(
+       'user' => array('type' => 'user', 'label' => t('User before receiving badge')),
+       'badge' => array('type' => 'userbadge', 'label' => t('The given Badge')),
+     ),
+     'module' => 'User Badges',
+   ),
+   'user_badges_badge_removed' => array(
+     'label' => t('User was removed a badge'),
+     'arguments' => array(
+       'user' => array('type' => 'user', 'label' => t('User before removing badge')),
+       'badge' => array('type' => 'userbadge', 'label' => t('The removed Badge')),
+     ),
+     'module' => 'User Badges',
+   ),
+ );
+}
+
+/**
+* Implementation of hook_rules_condition_info().
+*/
+function user_badges_rules_condition_info() {
+ return array(
+   'user_badges_has_badge' => array(
+     'label' => t('User has badge'),
+     'arguments' => array(
+       'account' => array('type' => 'user', 'label' => t('User')),
+     ),
+     'module' => 'User Badges',
+   ),
+   'user_badges_receives_badge' => array(
+     'label' => t('Badge user is receiving'),
+     'arguments' => array(
+       'badge' => array('type' => 'userbadge', 'label' => t('The given Badge')),
+     ),
+     'module' => 'User Badges',
+   ),
+   'user_badges_badge_count' => array(
+     'label' => t('User badge count'),
+     'arguments' => array(
+       'account' => array('type' => 'user', 'label' => t('User')),
+       'number' => array('type' => 'number', 'label' => t('Number')),
+     ),
+     'module' => 'User Badges',
+   ),
+   'user_badges_has_badge_by_name' => array(
+     'label' => t('User has badge by name'),
+     'arguments' => array(
+       'account' => array('type' => 'user', 'label' => t('User')),
+       'badgename' => array('type' => 'string', 'label' => t('Badge Name')),
+     ),
+     'module' => 'User Badges',
+   ),
+ );
+}
+
+/**
+* Configuration form for user_badges_has_badge condition
+*/
+function user_badges_has_badge_form($settings = array(), &$form) {
+ $form['badges'] = array('#tree' => TRUE);
+ $badges = user_badges_get_badges('all');
+ $settings['badges'] = is_array($settings['badges']) ? $settings['badges'] : array();
+
+ $form['settings']['operator'] = array(
+   '#type' => 'select',
+   '#title' => t('Operator'),
+   '#options' => array(
+     'any' => t('Has one of'),
+     'all' => t('Has all of'),
+   ),
+   '#default_value' => $settings['operator'],
+ );
+ foreach ($badges as $badge) {
+   $form['settings']['badges'][$badge->bid] = array(
+     '#type' => 'checkbox',
+     '#title' => theme('user_badge', $badge),
+     '#return_value' => 1,
+     '#default_value' => $settings['badges'][$badge->bid],
+     '#description' => check_plain($badge->name),
+   );
+ }
+
+ return $form;
+}
+
+/**
+* Test the user_badges_has_badge condition
+*/
+function user_badges_has_badge($account, $settings) {
+ $badges = user_badges_get_user_badges($account->uid, array('nolimit' => TRUE));
+ $result = ($settings['operator'] == 'all');
+ foreach ($settings['badges'] as $bid => $on) {
+   if (!$on) {
+     continue;
+   }
+   $has_badge = _user_badges_bid_in_badges($bid, $badges);
+   if ($settings['operator'] == 'all') {
+     $result = $result && $has_badge;
+   }
+   else {
+     $result = $result || $has_badge;
+   }
+ }
+ return $result;
+}
+
+/**
+* Configuration form for user_badges_receives_badge condition
+*/
+function user_badges_receives_badge_form($settings = array(), &$form) {
+ $form['badges'] = array('#tree' => TRUE);
+ $badges = user_badges_get_badges('all');
+ $settings['badges'] = is_array($settings['badges']) ? $settings['badges'] : array();
+
+ foreach ($badges as $badge) {
+   $form['settings']['badges'][$badge->bid] = array(
+     '#type' => 'radio',
+     '#title' => theme('user_badge', $badge),
+     '#return_value' => 1,
+     '#default_value' => $settings['badges'][$badge->bid],
+     '#description' => check_plain($badge->name),
+   );
+ }
+
+ return $form;
+}
+
+/**
+* Test the user_badges_receives_badge condition
+*/
+function user_badges_receives_badge($badge, $settings) {
+  $badge_bool = false;
+  foreach ($settings['badges'] as $bid => $on) {
+    if($bid == $badge->bid && $on == 1){
+      $badge_bool = true;
+      break;
+      }
+    }
+  return $badge_bool;
+}
+
+/**
+* Configuration form for user_badges_badge_count condition
+*/
+function user_badges_badge_count_form($settings = array(), &$form) {
+  $form['settings']['operation'] = array(
+    '#type' => 'select',
+    '#title' => t('User\'s number of badges is'),
+    '#options' => array('greater' => t('Greater than'), 'equal' => t('Equal to'), 'less' => t('Less than')),
+    '#default_value' => isset($settings['operation']) ? $settings['operation'] : 'equal',
+  );
+  $form['settings']['number']['#weight'] = 2;
+}
+
+/**
+* Test the user_badges_badge_count condition
+*/
+function user_badges_badge_count($account, $number, $settings) {
+ $badges = user_badges_get_user_badges($account->uid, array('nolimit' => TRUE));
+ $badges_count = count($badges);
+ switch ($settings['operation']) {
+    case 'greater':
+      return $badges_count > $number;
+
+    case 'equal':
+      return $badges_count == $number;
+
+    case 'less':
+      return $badges_count < $number;
+  }
+}
+
+/**
+ * Test the user_badges_has_badge_by_name condition
+ */
+function user_badges_has_badge_by_name($account, $badgename, $settings) {
+  $has_badge = FALSE;
+  $user_badges = user_badges_get_user_badges($account->uid, array('nolimit' => TRUE));
+  if(count($user_badges)){
+    foreach($user_badges as $badge){
+        if($badgename == $badge->name){
+            $has_badge = TRUE;
+            break;
+            }
+        }
+    }
+  return $has_badge;
+}
+
+/**
+ * Implements hook_rules_action_info().
+ */
+function user_badges_rules_action_info(){
+  return array(
+    'user_badges_action_add_badge_by_name' => array(
+      'label' => t('Add badge by name'),
+      'arguments' => array(
+        'account' => array('type' => 'user', 'label' => t('Acting User')),
+        ),
+      'module' => 'User',
+      'eval input' => array('badge'),
+      ),
+    'user_badges_action_remove_badge_by_name' => array(
+      'label' => t('Remove badge by name'),
+      'arguments' => array(
+        'account' => array('type' => 'user', 'label' => t('Acting User')),
+        ),
+      'module' => 'User',
+      'eval input' => array('badge'),
+      ),
+    );
+  }
+
+/**
+ * Implements a Rules action.
+ * Adds a badge to the current user, by the textual name of the badge.
+ */
+function user_badges_action_add_badge_by_name($account, $settings){
+    if(isset($account->uid)){$uid = $account->uid;}
+    else{global $user;$uid = $user->uid;}
+
+    $badges = user_badges_get_badges('all');
+    foreach($badges as $bid => $badge){
+        if($settings['badge'] == $badge->name){
+            user_badges_user_add_badge($uid, $bid, 'user');
+            break;
+            }
+        }
+    }
+
+function user_badges_action_add_badge_by_name_form($settings, &$form){
+    $settings += array('badge' => '');
+    $form['settings']['badge'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Badge name'),
+        '#default_value' => $settings['badge'],
+        '#description' => t('The textual name of the badge as configured on the user badges admin page.'),
+        );
+    }
+
+/**
+ * Implements a Rules action.
+ * Removes a badge from the current user, by the textual name of the badge.
+ */
+function user_badges_action_remove_badge_by_name($account, $settings){
+    if(isset($account->uid)){$uid = $account->uid;}
+    else{global $user;$uid = $user->uid;}
+
+    $badges = user_badges_get_user_badges($uid, array('nolimit' => TRUE));
+    foreach($badges as $ubid => $badge){
+        if($settings['badge'] == $badge->name){
+            user_badges_user_remove_badge($ubid);
+            break;
+            }
+        }
+    }
+
+function user_badges_action_remove_badge_by_name_form($settings, &$form){
+    $settings += array('badge' => '');
+    $form['settings']['badge'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Badge name'),
+        '#default_value' => $settings['badge'],
+        '#description' => t('The textual name of the badge as configured on the user badges admin page.'),
+        );
+    }
diff --git a/user_badges.views.inc b/user_badges.views.inc
index d7f05ba..21bce3c 100644
--- a/user_badges.views.inc
+++ b/user_badges.views.inc
@@ -75,7 +75,7 @@ function user_badges_views_data() {
   // Information for processing badge IDs
   $data['user_badges_user']['bid'] = array(
     'title' => t('Bid'),
-    'help' => t('The badge ID of the user badge itself.'),
+    'help' => t('The badge ID of the badge.'),
     'field' => array(
       'handler' => 'views_handler_field_numeric',
       'click sortable' => TRUE,
@@ -96,6 +96,54 @@ function user_badges_views_data() {
     ),
   );
 
+  // Information for processing user badge IDs
+  $data['user_badges_user']['ubid'] = array(
+    'title' => t('ubid'),
+    'help' => t('The ID of the user badge.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    // Information for accepting a ubid as an argument
+    'argument' => array(
+      'handler' => 'views_handler_argument_numeric',
+      'name field' => 'User Badge ID',
+      'numeric' => TRUE,
+    ),
+    //Info for filtering by ubid
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+    // Information for sorting on a ubid.
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  // Information for processing badge timestamps
+  $data['user_badges_user']['earned'] = array(
+    'title' => t('Date/Time earned'),
+    'help' => t('A timestamp when the user badge was earned.'),
+    'field' => array(
+      'handler' => 'views_handler_field_date',
+      'click sortable' => TRUE,
+    ),
+    // Information for accepting earned as an argument
+    'argument' => array(
+      'handler' => 'views_handler_argument_date',
+      'name field' => 'Earned Timestamp',
+      'numeric' => TRUE,
+    ),
+    //Info for filtering by earned
+    'filter' => array(
+      'handler' => 'views_handler_filter_date',
+    ),
+    // Information for sorting on earned.
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
   // Information for processing badge types
   $data['user_badges_user']['type'] = array(
     'title' => t('Type'),
@@ -153,7 +201,7 @@ function user_badges_views_data() {
 
   $data['user_badges_badges']['badge'] = array(
     'title' => t('Badge'),
-    'real field' => 'bid',
+    'real field' => 'ubid',
     'help' => t('An individual user badge.'),
     'field' => array(
       'handler' => 'views_handler_field_user_badges_badges_badge',
diff --git a/user_badges_products.info b/user_badges_products.info
deleted file mode 100644
index 504c926..0000000
--- a/user_badges_products.info
+++ /dev/null
@@ -1,7 +0,0 @@
-
-name = User Product Badges
-description = Enables assignment of graphical badges to users and roles.
-dependencies[] = user_badges
-dependencies[] = ec_product
-
-core = 6.x
diff --git a/user_badges_products.install b/user_badges_products.install
deleted file mode 100644
index 5860b06..0000000
--- a/user_badges_products.install
+++ /dev/null
@@ -1,58 +0,0 @@
-<?php
-
-/**
- * @file
- * @brief User Badges Product install file
- *
- * This file contains all the installation functions of the schema, tables and variables
- * used by the module.
- *
- * @author Nuno Veloso (nunoveloso18), http://drupal.org/user/80656
- * @author Richard Skinner (Likeless), http://drupal.org/user/310635
- *
- * @warning For more information on licensing, read the LICENCE.txt file.
- *
- */
-
-/**
- * Implements hook_schema().
- */
-function user_badges_products_schema() {
-  $schema['user_badges_product'] = array(
-    'description' => 'Stores which products grant which badges.',
-    'fields' => array(
-      'bid' => array(
-        'description' => t('Badge ID'),
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0
-      ),
-      'nid' => array(
-        'description' => t('Node ID'),
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-    ),
-    'primary key' => array('bid', 'nid'),
-  );
-
-  return $schema;
-}
-
-/**
- * Implements hook_install().
- */
-function user_badges_products_install() {
-  drupal_install_schema('user_badges_products');
-}
-
-/**
- * Implements hook_uninstall().
- */
-function user_badges_products_uninstall() {
-  drupal_uninstall_schema('user_badges_products');
-}
-
diff --git a/user_badges_products.module b/user_badges_products.module
deleted file mode 100644
index f5638ba..0000000
--- a/user_badges_products.module
+++ /dev/null
@@ -1,222 +0,0 @@
-<?php
-
-/**
- * @file
- * @brief User Badges Product module file
- * 
- * This file contains all the functions used by the module.
- *
- * @author Jeff Robbins (jjeff), http://drupal.org/user/17190
- * @author Chad Phillips (hunmonk), http://drupal.org/user/22079
- * @author Heine Deelstra (Heine), http://drupal.org/user/17943
- * @author Nuno Veloso (nunoveloso18), http://drupal.org/user/80656
- * @author Richard Skinner (Likeless), http://drupal.org/user/310635
- *
- * @warning For more information on licensing, read the LICENCE.txt file.
- *
- */
-
-/**
- * Implementation of hook_help().
- */
-function user_badges_products_help($path, $arg) {
-  switch ($path) {
-    case 'admin/user/user_badges/products':
-      return t("For each ecommerce product listed below, select the badge that will be assigned to users upon payment completion.");
-  }
-}
-
-/**
- * Implementation of hook_menu().
- */
-function user_badges_products_menu() {
-  $items['admin/user/user_badges/products'] = array(
-    'title' => 'Products',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('user_badges_products_list_form'),
-    'access arguments' => array('manage badges'),
-    'weight' => 8,
-    'type' => MENU_LOCAL_TASK,
-  );
-  return $items;
-}
-
-/**
- * Implementation of hook_theme().
- */
-function user_badges_products_theme() {
-  return array(
-    'user_badges_products_list_form' => array(
-      'arguments' => array('form' => NULL),
-    ),
-  );
-}
-
-/**
- * Form builder; list of badges
- */
-function user_badges_products_list_form() {
-  
-  // load the badges that we want to display
-  $form['header'] = array('#type' => 'value', '#value' => array(
-    array('data' => t('Product'), 'field' => 'title', 'sort' => 'asc'),
-    array('data' => t('Badge'), 'field' => 'weight'),
-    array('data' => t('Remove')),
-    array('data' => t('New Badge')),
-  ));
-  
-  $result = pager_query('SELECT ubb.*, n.title, n.nid FROM {ec_product} p INNER JOIN {node} n ON p.nid = n.nid LEFT JOIN {user_badges_product} ubp ON ubp.nid=p.nid LEFT JOIN {user_badges_badges} ubb ON ubp.bid=ubb.bid'. tablesort_sql($form['header']['#value']), 50 );
-
-  // build a table listing the appropriate badges
-  while ($badgeprod = db_fetch_object($result)) {
-    $form['name'][$badgeprod->nid] = array('#value' => check_plain($badgeprod->title));
-    $form['newbadge'][$badgeprod->nid] = array('#type' => 'textfield', '#size' => 40, '#maxlength' => 255, '#autocomplete_path' => 'user_badges/autocomplete' );
-    //Only make these elements active if there is a badge
-    if ($badgeprod->bid) {
-      $form['badge'][$badgeprod->nid] = array('#value' => theme('user_badge', $badgeprod));
-      $form['clear']['clear'. $badgeprod->nid] = array('#type' => 'checkbox', '#return_value' => 1, '#default_value' => 0);
-    }
-    else {
-      $form['badge'][$badgeprod->nid] = array('#value' => ' ');
-      $form['clear']['clear'. $badgeprod->nid] = array('#value' => ' ');
-    }
-  }
-  
-  $form['submit'] = array('#type' => 'submit', '#value' => t('Update'));
-  
-  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
-  
-  return $form;
-}
-
-
-/**
- * Validate user_badges_products_list_form form submissions.
- *
- * All weights should be numeric
- */
-function user_badges_products_list_form_validate($form, &$form_state) {
-  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
-    foreach (element_children($form['newbadge']) as $nid) {
-      if (!empty($form_state['values'][$nid])) {
-        //The field isn't empty, so we should validate it
-        $validation = user_badges_badge_autocomplete_validation($form_state['values'][$nid]);
-        //Is it correctly formatted?
-        if ($validation[1] == 'string') {
-          form_set_error($nid, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $form_state['values'][$nid])));
-        }
-        //The format was correct, but we need to check the bid exists
-        elseif ($validation[1] == 'nobid') {
-          form_set_error($nid, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
-        }
-      }
-    }
-  }
-}
-
-/**
- * Process user_badges_products_list_form form submissions.
- *
- * Update the badge weights
- */
-function user_badges_products_list_form_submit($form, &$form_state) {
-  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
-    foreach (element_children($form['newbadge']) as $nid) {
-      //Remove any old badge if set to clear
-      if ($form_state['values']['clear'.$nid]) {
-        db_query('DELETE FROM {user_badges_product} WHERE nid=%d', $nid);
-      }
-      //Add any new badge if one has been entered
-      if (!empty($form_state['values'][$nid])) {
-        $validation = user_badges_badge_autocomplete_validation($form_state['values'][$nid]);
-        db_query('DELETE FROM {user_badges_product} WHERE nid=%d', $nid);
-        db_query('INSERT INTO {user_badges_product} (nid, bid) VALUES (%d, %d)', $nid, $validation[0]);
-      }
-    }
-    drupal_set_message(t('The badge product associations have been updated.'));
-  }
-}
-
-/**
- * Theme the badge list form.
- *
- * @param $form
- *   An associative array containing the structure of the form.
- * @ingroup themeable
- */
-function theme_user_badges_products_list_form($form) {
-  
-  //Loop through the array items in the name array to get all the bids for our listed badges
-  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
-    foreach (element_children($form['newbadge']) as $key) {
-      //We only want nids as values of $key
-      if (!is_numeric($key)) {
-        continue;
-      }
-      
-      //Create the rows array for the table theme
-      $row = array();
-      $row[] = drupal_render($form['name'][$key]);
-      $row[] = drupal_render($form['badge'][$key]);
-      $row[] = drupal_render($form['clear']['clear'. $key]);
-      $row[] = drupal_render($form['newbadge'][$key]);
-      $rows[] = $row;
-    }
-    
-    //Add the submit button
-    $row = array();
-    $row[] = '';
-    $row[] = '';
-    $row[] = '';
-    $row[] = drupal_render($form['submit']);
-    $rows[] = $row;
-    
-  }
-  else {
-    $rows[] = array(array('data' => t('No products available.'), 'colspan' => '4'));
-  }
-  
-  //Theme all that we have processed so far into a table
-  $output .= theme('table', $form['header']['#value'], $rows);
-  
-  //Create the table's pager
-  if ($form['pager']['#value']) {
-    $output .= drupal_render($form['pager']);
-  }
-  
-  //Render any remaining form elements
-  $output .= drupal_render($form);
-  
-  return $output;
-}
-
-/**
- * Implementation of hook_ecommerceapi().
- */
-function user_badges_products_ecommerceapi($t, $op) {
-  switch ($op) {
-    case 'on payment completion':
-      $productbadges = user_badges_products_get_products();
-      foreach ($t['items'] as $item) {
-        if (array_key_exists($item->nid, $productbadges)) {
-          // no duplicates please...
-          db_query("DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d", $t['uid'], $productbadges[$item->nid]);
-          db_query("INSERT INTO {user_badges_user} (uid, bid, type) VALUES (%d, %d, 'product')", $t['uid'], $productbadges[$item->nid]);
-        }
-      }
-  }
-}
-
-/**
-  * Get list of products that have badges
-  * keys are node ids (nid)
-  * values are badge ids (bid)
-  */
-function user_badges_products_get_products() {
-  $products = array();
-  $sql = db_query('SELECT * FROM {user_badges_product}');
-  while ($row = db_fetch_object($sql)) {
-    $products[$row->nid] = $row->bid;
-  }
-  return $products;
-}
diff --git a/user_badges_products/user_badges_products.info b/user_badges_products/user_badges_products.info
new file mode 100644
index 0000000..504c926
--- /dev/null
+++ b/user_badges_products/user_badges_products.info
@@ -0,0 +1,7 @@
+
+name = User Product Badges
+description = Enables assignment of graphical badges to users and roles.
+dependencies[] = user_badges
+dependencies[] = ec_product
+
+core = 6.x
diff --git a/user_badges_products/user_badges_products.install b/user_badges_products/user_badges_products.install
new file mode 100644
index 0000000..5860b06
--- /dev/null
+++ b/user_badges_products/user_badges_products.install
@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * @file
+ * @brief User Badges Product install file
+ *
+ * This file contains all the installation functions of the schema, tables and variables
+ * used by the module.
+ *
+ * @author Nuno Veloso (nunoveloso18), http://drupal.org/user/80656
+ * @author Richard Skinner (Likeless), http://drupal.org/user/310635
+ *
+ * @warning For more information on licensing, read the LICENCE.txt file.
+ *
+ */
+
+/**
+ * Implements hook_schema().
+ */
+function user_badges_products_schema() {
+  $schema['user_badges_product'] = array(
+    'description' => 'Stores which products grant which badges.',
+    'fields' => array(
+      'bid' => array(
+        'description' => t('Badge ID'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0
+      ),
+      'nid' => array(
+        'description' => t('Node ID'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'primary key' => array('bid', 'nid'),
+  );
+
+  return $schema;
+}
+
+/**
+ * Implements hook_install().
+ */
+function user_badges_products_install() {
+  drupal_install_schema('user_badges_products');
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function user_badges_products_uninstall() {
+  drupal_uninstall_schema('user_badges_products');
+}
+
diff --git a/user_badges_products/user_badges_products.module b/user_badges_products/user_badges_products.module
new file mode 100644
index 0000000..f5638ba
--- /dev/null
+++ b/user_badges_products/user_badges_products.module
@@ -0,0 +1,222 @@
+<?php
+
+/**
+ * @file
+ * @brief User Badges Product module file
+ * 
+ * This file contains all the functions used by the module.
+ *
+ * @author Jeff Robbins (jjeff), http://drupal.org/user/17190
+ * @author Chad Phillips (hunmonk), http://drupal.org/user/22079
+ * @author Heine Deelstra (Heine), http://drupal.org/user/17943
+ * @author Nuno Veloso (nunoveloso18), http://drupal.org/user/80656
+ * @author Richard Skinner (Likeless), http://drupal.org/user/310635
+ *
+ * @warning For more information on licensing, read the LICENCE.txt file.
+ *
+ */
+
+/**
+ * Implementation of hook_help().
+ */
+function user_badges_products_help($path, $arg) {
+  switch ($path) {
+    case 'admin/user/user_badges/products':
+      return t("For each ecommerce product listed below, select the badge that will be assigned to users upon payment completion.");
+  }
+}
+
+/**
+ * Implementation of hook_menu().
+ */
+function user_badges_products_menu() {
+  $items['admin/user/user_badges/products'] = array(
+    'title' => 'Products',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('user_badges_products_list_form'),
+    'access arguments' => array('manage badges'),
+    'weight' => 8,
+    'type' => MENU_LOCAL_TASK,
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_theme().
+ */
+function user_badges_products_theme() {
+  return array(
+    'user_badges_products_list_form' => array(
+      'arguments' => array('form' => NULL),
+    ),
+  );
+}
+
+/**
+ * Form builder; list of badges
+ */
+function user_badges_products_list_form() {
+  
+  // load the badges that we want to display
+  $form['header'] = array('#type' => 'value', '#value' => array(
+    array('data' => t('Product'), 'field' => 'title', 'sort' => 'asc'),
+    array('data' => t('Badge'), 'field' => 'weight'),
+    array('data' => t('Remove')),
+    array('data' => t('New Badge')),
+  ));
+  
+  $result = pager_query('SELECT ubb.*, n.title, n.nid FROM {ec_product} p INNER JOIN {node} n ON p.nid = n.nid LEFT JOIN {user_badges_product} ubp ON ubp.nid=p.nid LEFT JOIN {user_badges_badges} ubb ON ubp.bid=ubb.bid'. tablesort_sql($form['header']['#value']), 50 );
+
+  // build a table listing the appropriate badges
+  while ($badgeprod = db_fetch_object($result)) {
+    $form['name'][$badgeprod->nid] = array('#value' => check_plain($badgeprod->title));
+    $form['newbadge'][$badgeprod->nid] = array('#type' => 'textfield', '#size' => 40, '#maxlength' => 255, '#autocomplete_path' => 'user_badges/autocomplete' );
+    //Only make these elements active if there is a badge
+    if ($badgeprod->bid) {
+      $form['badge'][$badgeprod->nid] = array('#value' => theme('user_badge', $badgeprod));
+      $form['clear']['clear'. $badgeprod->nid] = array('#type' => 'checkbox', '#return_value' => 1, '#default_value' => 0);
+    }
+    else {
+      $form['badge'][$badgeprod->nid] = array('#value' => ' ');
+      $form['clear']['clear'. $badgeprod->nid] = array('#value' => ' ');
+    }
+  }
+  
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Update'));
+  
+  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
+  
+  return $form;
+}
+
+
+/**
+ * Validate user_badges_products_list_form form submissions.
+ *
+ * All weights should be numeric
+ */
+function user_badges_products_list_form_validate($form, &$form_state) {
+  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
+    foreach (element_children($form['newbadge']) as $nid) {
+      if (!empty($form_state['values'][$nid])) {
+        //The field isn't empty, so we should validate it
+        $validation = user_badges_badge_autocomplete_validation($form_state['values'][$nid]);
+        //Is it correctly formatted?
+        if ($validation[1] == 'string') {
+          form_set_error($nid, t('"@value" is not a valid badge name. Try using the autocomplete function (requires javascript).', array('@value' => $form_state['values'][$nid])));
+        }
+        //The format was correct, but we need to check the bid exists
+        elseif ($validation[1] == 'nobid') {
+          form_set_error($nid, t('@value is not a valid badge ID. Try using the autocomplete function (requires javascript).', array('@value' => $validation[0])));
+        }
+      }
+    }
+  }
+}
+
+/**
+ * Process user_badges_products_list_form form submissions.
+ *
+ * Update the badge weights
+ */
+function user_badges_products_list_form_submit($form, &$form_state) {
+  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
+    foreach (element_children($form['newbadge']) as $nid) {
+      //Remove any old badge if set to clear
+      if ($form_state['values']['clear'.$nid]) {
+        db_query('DELETE FROM {user_badges_product} WHERE nid=%d', $nid);
+      }
+      //Add any new badge if one has been entered
+      if (!empty($form_state['values'][$nid])) {
+        $validation = user_badges_badge_autocomplete_validation($form_state['values'][$nid]);
+        db_query('DELETE FROM {user_badges_product} WHERE nid=%d', $nid);
+        db_query('INSERT INTO {user_badges_product} (nid, bid) VALUES (%d, %d)', $nid, $validation[0]);
+      }
+    }
+    drupal_set_message(t('The badge product associations have been updated.'));
+  }
+}
+
+/**
+ * Theme the badge list form.
+ *
+ * @param $form
+ *   An associative array containing the structure of the form.
+ * @ingroup themeable
+ */
+function theme_user_badges_products_list_form($form) {
+  
+  //Loop through the array items in the name array to get all the bids for our listed badges
+  if (isset($form['newbadge']) && is_array($form['newbadge'])) {
+    foreach (element_children($form['newbadge']) as $key) {
+      //We only want nids as values of $key
+      if (!is_numeric($key)) {
+        continue;
+      }
+      
+      //Create the rows array for the table theme
+      $row = array();
+      $row[] = drupal_render($form['name'][$key]);
+      $row[] = drupal_render($form['badge'][$key]);
+      $row[] = drupal_render($form['clear']['clear'. $key]);
+      $row[] = drupal_render($form['newbadge'][$key]);
+      $rows[] = $row;
+    }
+    
+    //Add the submit button
+    $row = array();
+    $row[] = '';
+    $row[] = '';
+    $row[] = '';
+    $row[] = drupal_render($form['submit']);
+    $rows[] = $row;
+    
+  }
+  else {
+    $rows[] = array(array('data' => t('No products available.'), 'colspan' => '4'));
+  }
+  
+  //Theme all that we have processed so far into a table
+  $output .= theme('table', $form['header']['#value'], $rows);
+  
+  //Create the table's pager
+  if ($form['pager']['#value']) {
+    $output .= drupal_render($form['pager']);
+  }
+  
+  //Render any remaining form elements
+  $output .= drupal_render($form);
+  
+  return $output;
+}
+
+/**
+ * Implementation of hook_ecommerceapi().
+ */
+function user_badges_products_ecommerceapi($t, $op) {
+  switch ($op) {
+    case 'on payment completion':
+      $productbadges = user_badges_products_get_products();
+      foreach ($t['items'] as $item) {
+        if (array_key_exists($item->nid, $productbadges)) {
+          // no duplicates please...
+          db_query("DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d", $t['uid'], $productbadges[$item->nid]);
+          db_query("INSERT INTO {user_badges_user} (uid, bid, type) VALUES (%d, %d, 'product')", $t['uid'], $productbadges[$item->nid]);
+        }
+      }
+  }
+}
+
+/**
+  * Get list of products that have badges
+  * keys are node ids (nid)
+  * values are badge ids (bid)
+  */
+function user_badges_products_get_products() {
+  $products = array();
+  $sql = db_query('SELECT * FROM {user_badges_product}');
+  while ($row = db_fetch_object($sql)) {
+    $products[$row->nid] = $row->bid;
+  }
+  return $products;
+}
diff --git a/user_badges_stats/user_badges_stats.info b/user_badges_stats/user_badges_stats.info
new file mode 100644
index 0000000..c2d4253
--- /dev/null
+++ b/user_badges_stats/user_badges_stats.info
@@ -0,0 +1,7 @@
+
+name = User Badge Stats
+description = Provides queryable statistics on user badges
+dependencies[] = user_badges
+dependencies[] = user_stats
+
+core = 6.x
diff --git a/user_badges_stats/user_badges_stats.install b/user_badges_stats/user_badges_stats.install
new file mode 100644
index 0000000..f97fc43
--- /dev/null
+++ b/user_badges_stats/user_badges_stats.install
@@ -0,0 +1,11 @@
+<?php
+
+/**
+ * @file
+ * @brief User Badge Stats install file
+ *
+ * This file contains all the installation functions of the schema, tables and variables
+ * used by the module.
+ *
+ * @author Brad Czerniak (ao5357), http://drupal.org/user/313975
+ */
\ No newline at end of file
diff --git a/user_badges_stats/user_badges_stats.module b/user_badges_stats/user_badges_stats.module
new file mode 100644
index 0000000..beee4a1
--- /dev/null
+++ b/user_badges_stats/user_badges_stats.module
@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * @file
+ * @brief User Badge Stats module file
+ * 
+ * This file contains all the functions used by the module.
+ *
+ * @author Brad Czerniak (ao5357), http://drupal.org/user/313975
+ */
diff --git a/views_handler_field_user_badges_badges_badge.inc b/views_handler_field_user_badges_badges_badge.inc
index e9726d3..def3fce 100644
--- a/views_handler_field_user_badges_badges_badge.inc
+++ b/views_handler_field_user_badges_badges_badge.inc
@@ -36,6 +36,4 @@ class views_handler_field_user_badges_badges_badge extends views_handler_field {
     //Send it through the standard theme and return it
     return theme('user_badge', $badge);
   }
-  
-  
 }
diff --git a/views_handler_field_user_badges_user_uid.inc b/views_handler_field_user_badges_user_uid.inc
index 368a9fb..a9774e1 100644
--- a/views_handler_field_user_badges_user_uid.inc
+++ b/views_handler_field_user_badges_user_uid.inc
@@ -20,7 +20,7 @@ class views_handler_field_user_badges_user_uid extends views_handler_field {
       //For the text options, get the badge objects and format their names
       case 'linked_text':
       case 'unlinked_text':
-        $user_badges = user_badges_get_badges($uid);
+        $user_badges = user_badges_get_user_badges($uid, array('nolimit' => TRUE));
         
         //If we have no badges for the user, just return nothing.
         if (count($user_badges) == 0) {
@@ -40,7 +40,7 @@ class views_handler_field_user_badges_user_uid extends views_handler_field {
       
       //Send the array of badge objects to the custom theme function
       case 'custom_theme':
-        $user_badges = user_badges_get_badges($uid);
+        $user_badges = user_badges_get_user_badges($uid, array('nolimit' => TRUE));
         return theme($this->options['theme'], $user_badges);
         
       //This case is for everything else. It returns the normal themed badges.
