diff -urp avatar_selection_old/avatar_selection.admin.inc avatar_selection/avatar_selection.admin.inc
--- avatar_selection_old/avatar_selection.admin.inc	2011-02-25 12:07:25.000000000 +1000
+++ avatar_selection/avatar_selection.admin.inc	2012-02-02 10:38:00.000000000 +1000
@@ -43,7 +43,9 @@ function _avatar_selection_scan_images($
       unset($avatars[$filename]);
     }
     else {
-      _avatar_selection_save_avatar_info(0, $filename, (empty($name) ?  $filename : $name), $access, $og, $weight);
+      //manage the file and add it to the avatar table
+      $file = file_save(_avatar_selection_uri_to_object("$dir/" . $listing->filename));
+      _avatar_selection_save_avatar_info(0, $filename, (empty($name) ?  $filename : $name), $access, $og, $weight, $file->fid);
       $add_count++;
     }
   }
@@ -51,7 +53,8 @@ function _avatar_selection_scan_images($
   // Remove records from database where we have an avatar entry but no
   // corresponding file.
   foreach ($avatars as $avatar) {
-    avatar_selection_image_delete($avatar);
+    $file = _avatar_selection_uri_to_object("$dir/" . $avatar, TRUE);
+    avatar_selection_image_delete($file->fid);
     $delete_count++;
   }
 
@@ -63,6 +66,50 @@ function _avatar_selection_scan_images($
 
 
 /**
+ * Create a file object from a file uri
+ *
+ * @param $uri
+ *   A file uri of an existing unmanaged file
+ * @return
+ *   A file object
+ */
+function _avatar_selection_uri_to_object($uri, $use_existing = FALSE) {
+  if ($use_existing) {
+    $query = db_select('file_managed', 'f')
+      ->fields('f', array('fid'))
+      ->condition('uri', $uri)
+      ->execute()
+      ->fetchCol();
+    if (!empty($query)) {
+      $file = file_load(array_shift($query));
+    }
+  }
+  if (!isset($file)) {
+    global $user;
+    $uri = file_stream_wrapper_uri_normalize($uri);
+    $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
+    $file = new StdClass;
+    $file->uid = $user->uid;
+    $file->filename = basename($uri);
+    $file->uri = $uri;
+    $file->filemime = file_get_mimetype($uri);
+    // This is gagged because some uris will not support it.
+    $file->filesize = @filesize($uri);
+    $file->timestamp = REQUEST_TIME;
+    $file->status = FILE_STATUS_PERMANENT;
+    $file->is_new = TRUE;
+  }
+  return $file;
+}
+
+
+
+
+
+
+
+
+/**
  * Select which form will be shown to the user, according to the permissions.
  *
  * @param $op
@@ -355,8 +402,6 @@ function avatar_selection_roles_page($op
  *   Return the structure of the form.
  */
 function avatar_selection_edit_form($form, $form_state) {
-  // We need the pager global variables.
-  global $_GET;
 
   $form = array();
 
@@ -368,7 +413,7 @@ function avatar_selection_edit_form($for
 
   $set_type = arg(5);
   $set_id = arg(6);
-
+  
   // We find out the current page number.
   $page = 0;
   if (isset($_GET['page']) && is_numeric($_GET['page'])) {
@@ -435,7 +480,7 @@ function avatar_selection_edit_form($for
       $weight = 0;
       $name = '';
 
-      $result = db_query("SELECT avs.aid, avatar, name, weight, rid, ogid FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE avatar = :avatar ORDER BY weight, name, avatar", array(':avatar' => $form_state['values']['select_avatar']));
+      $result = db_query("SELECT avs.aid, avatar, name, weight, rid, ogid FROM {avatar_selection} avs LEFT JOIN {avatar_selection_roles} avsr ON avs.aid = avsr.aid LEFT JOIN {avatar_selection_og} avso ON avs.aid = avso.aid WHERE fid = :avatar ORDER BY weight, name, avatar", array(':avatar' => $form_state['values']['select_avatar']));
       foreach ($result as $avatar) {
         $aid = $avatar->aid;
         $name = $avatar->name;
@@ -450,9 +495,13 @@ function avatar_selection_edit_form($for
 
       $image_path = file_build_uri('avatar_selection');
       file_prepare_directory($image_path, FILE_CREATE_DIRECTORY);
-      $selected_avatar = $form_state['values']['select_avatar'];
+      $selected_avatar = $avatar->avatar;
       $image = theme('image', array('path' => $image_path . '/' . $selected_avatar));
-      $form['avatar_image'] = array('#value' => $image);
+      $form['avatar_image'] = array(
+        '#type' => 'item',
+        '#title' => t('Image'),
+        '#markup' => $image,
+      );
       $form['aid'] = array(
         '#type' => 'value',
         '#value' => $aid,
@@ -633,13 +682,13 @@ function avatar_selection_upload_form_su
       if ($file = file_save_upload('picture_upload', $validators, $dir, FILE_EXISTS_RENAME)) {
 
         if (image_get_info($file->uri)) {
-          $file->status &= FILE_STATUS_PERMANENT;
+          $file->status = FILE_STATUS_PERMANENT;
           $file = file_save($file);
-          _avatar_selection_save_avatar_info(0, $file->filename, $name, $access, $og, $weight);
+          _avatar_selection_save_avatar_info(0, $file->filename, $name, $access, $og, $weight, $file->fid);
           drupal_set_message(t('New image saved.'));
         }
         else {
-          file_delete($file->uri);
+          file_delete($file);
           drupal_set_message(t('Uploaded file does not appear to be a valid image file. Please try again.'));
         }
       }
@@ -705,8 +754,8 @@ function avatar_selection_edit_update_fo
  *   General reference, used to control the processing of the form.
  */
 function avatar_selection_edit_delete_form_submit($form, &$form_state) {
-  $image = $form_state['values']['select_avatar'];
-  $deleted = avatar_selection_image_delete($image);
+  $fid = $form_state['values']['select_avatar'];
+  $deleted = avatar_selection_image_delete($fid);
   if ($deleted) {
     drupal_set_message(t('Image deleted.'));
   }
@@ -715,31 +764,27 @@ function avatar_selection_edit_delete_fo
 /**
  * Delete the specified avatar image.
  *
- * @param $image
- *   Path to the image to be deleted.
+ * @param $fid
+ *   The image file id to be deleted.
  */
-function avatar_selection_image_delete($image) {
-  $dir = file_build_uri('avatar_selection');
-  file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
-  if (file_check_location($dir . '/' . $image, $dir)) {
-    $aid = db_query("SELECT aid FROM {avatar_selection} WHERE avatar = :avatar", array(':avatar' => $image))->fetchField();
-    if ($aid) {
-      $result = db_delete('avatar_selection')
-        ->condition('aid', $aid)
-        ->execute();
-      $result = db_delete('avatar_selection_roles')
-        ->condition('aid', $aid)
-        ->execute();
-      $result = db_delete('avatar_selection_og')
-        ->condition('aid', $aid)
-        ->execute();
-      file_delete($dir . '/' . $image);
-      if (!image_get_info($dir . '/' . $image)) {
-        return 1;
-      }
-    }
+function avatar_selection_image_delete($fid) {
+
+  file_delete(file_load($fid));
+
+  $aid = db_query("SELECT aid FROM {avatar_selection} WHERE fid = :avatar", array(':avatar' => $fid))->fetchField();
+  if ($aid) {
+    $result = db_delete('avatar_selection')
+      ->condition('aid', $aid)
+      ->execute();
+    $result = db_delete('avatar_selection_roles')
+      ->condition('aid', $aid)
+      ->execute();
+    $result = db_delete('avatar_selection_og')
+      ->condition('aid', $aid)
+      ->execute();
   }
-  return 0;
+
+  return TRUE;
 }
 
 /**
@@ -760,8 +805,10 @@ function avatar_selection_image_delete($
  * @param $weight
  *   The weight of the avatar.  Lower weighted avatars appear before higher
  *   weighted avatars in the list.
+ * @param $fid
+ *   The managed file id
  */
-function _avatar_selection_save_avatar_info($aid, $image, $name, $access, $og = array(), $weight = 0) {
+function _avatar_selection_save_avatar_info($aid, $image, $name, $access, $og = array(), $weight = 0, $fid = NULL) {
 
   // Add or update avatar_selection table.
   if ($aid) {
@@ -786,6 +833,7 @@ function _avatar_selection_save_avatar_i
         'avatar' => $image,
         'name' => $name,
         'weight' => $weight,
+        'fid' => $fid,
       ))
       ->execute();
   }
diff -urp avatar_selection_old/avatar_selection.install avatar_selection/avatar_selection.install
--- avatar_selection_old/avatar_selection.install	2011-02-25 12:07:25.000000000 +1000
+++ avatar_selection/avatar_selection.install	2012-01-04 14:23:00.000000000 +1000
@@ -26,7 +26,6 @@ function avatar_selection_schema() {
       'fid' => array(
         'type' => 'int',
         'unsigned' => TRUE,
-        'not null' => TRUE,
         'description' => 'File identifier.',
       ),
       'avatar' => array(
diff -urp avatar_selection_old/avatar_selection.module avatar_selection/avatar_selection.module
--- avatar_selection_old/avatar_selection.module	2011-02-25 12:07:25.000000000 +1000
+++ avatar_selection/avatar_selection.module	2012-02-02 11:26:00.000000000 +1000
@@ -158,9 +158,7 @@ function avatar_selection_menu() {
  * @return
  *   Return the structure of the form.
  */
-function avatar_selection_form_user_register_alter(&$form, $form_state, $form_id = 'user_register') {
-  // We need the pager global variables.
-  global $_GET;
+function avatar_selection_form_user_register_form_alter(&$form, &$form_state, $form_id = 'user_register') {
 
   // If user pictures aren't enabled, nothing to do here.
   if (!variable_get('user_pictures', 0)) {
@@ -178,7 +176,7 @@ function avatar_selection_form_user_regi
     }
     return;
   }
-
+  
   // We find out the current page number.
   $page = 0;
   if (isset($_GET['page']) && is_numeric($_GET['page'])) {
@@ -200,6 +198,7 @@ function avatar_selection_form_user_regi
         '#weight' => 1,
       );
     }
+
     drupal_add_js(drupal_get_path('module', 'avatar_selection') . '/js/avatar_selection_pager.js');
     $form['picture']['select_avatar'] = array(
       '#type' => 'radios',
@@ -211,6 +210,7 @@ function avatar_selection_form_user_regi
       '#prefix' => '<div id="avatar-selection-loading"></div>',
       '#suffix' => theme('avatar_selection_pager', array('form_id' => 'form#user-register', 'class' => 'div.user-avatar-select', 'total' => $selects['total'], 'limit' => $avatars_per_page)),
     );
+    $form['picture']['#access'] = TRUE;
     $form['#validate'][] = 'avatar_selection_validate_user_avatar';
   }
   else {
@@ -343,7 +343,14 @@ function avatar_selection_validate_user_
   if (!$file && !empty($form_state['values']['select_avatar'])) {
     unset($form_state['values']['picture_delete']);
     $path = file_build_uri('avatar_selection') . '/';
-    form_set_value($form['picture'], $path . $form_state['values']['select_avatar'], $form_state);
+    
+    //load file
+    $file = file_load($form_state['values']['select_avatar']);
+    $file->status = 0;
+    $file = file_copy($file, 'temporary://' . $file->filename);
+    
+    $form_state['values']['picture_upload'] = $file;
+    
   }
   // No file upload, no avatar selection picked, but random avatar selection image
   // allowed.
@@ -417,7 +424,7 @@ function _avatar_selection_image_list($u
 
   // Prepare base query.
   $count_query = db_select('avatar_selection', 'avs')->distinct();
-  $query = db_select('avatar_selection', 'avs')->fields('avs', array('avatar', 'name', 'weight'))->distinct()
+  $query = db_select('avatar_selection', 'avs')->fields('avs', array('avatar', 'fid', 'name', 'weight'))->distinct()
     ->orderBy('weight', 'ASC')
     ->orderBy('name', 'ASC')
     ->orderBy('avatar', 'ASC');
@@ -473,26 +480,27 @@ function _avatar_selection_image_list($u
     }
 
     // Distinct avatars are enabled - join users and avatar_selection_roles tables.
-    if (variable_get('avatar_selection_distinctive_avatars', FALSE)) {
-      $count_user_alias = $count_query->join('users', 'u', 'u.picture = concat(:dir, "/", avs.avatar)', array(':dir', $dir));
+    if (variable_get('avatar_selection_distinctive_avatars', FALSE)) {    
+      $count_user_alias = $count_query->join('users', 'u', 'u.picture = avs.fid');
       $count_query->isNull('u.picture');
       $count_avsr_alias = $count_query->join('avatar_selection_roles', 'avs', 'avs.aid = avsr.aid');
       $count_query->condition(db_or()->isNULL("{$count_avsr_alias}.rid")->condition("{$count_avsr_alias}.rid", $user_roles, 'IN'));
 
-      $user_alias = $query->join('users', 'u', 'u.picture = concat(:dir, "/", avs.avatar)', array(':dir', $dir));
+      $user_alias = $query->join('users', 'u', 'u.picture = avs.fid');
       $query->isNull('u.picture');
-      $avsr_alias = $query->join('avatar_selection_roles', 'avs', 'avs.aid = avsr.aid');
+      $avsr_alias = $query->join('avatar_selection_roles', 'avsr', 'avs.aid = avsr.aid');
       $query->condition(db_or()->isNULL("{$avsr_alias}.rid")->condition("{$avsr_alias}.rid", $user_roles, 'IN'));
     }
 
+    //This is disabled for now, as images that apply to all roles are ignored.
     // Not root user - join avatar_selection_roles table.
-    elseif ($user->uid != 1) {
-      $count_avsr_alias = $count_query->join('avatar_selection_roles', 'avs', 'avs.aid = avsr.aid');
+    /*elseif ($user->uid != 1) {
+      $count_avsr_alias = $count_query->join('avatar_selection_roles', 'avsr', 'avs.aid = avsr.aid');
       $count_query->condition(db_or()->isNULL("{$count_avsr_alias}.rid")->condition("{$count_avsr_alias}.rid", $user_roles, 'IN'));
 
-      $avsr_alias = $query->join('avatar_selection_roles', 'avs', 'avs.aid = avsr.aid');
+      $avsr_alias = $query->join('avatar_selection_roles', 'avsr', 'avs.aid = avsr.aid');
       $query->condition(db_or()->isNULL("{$avsr_alias}.rid")->condition("{$avsr_alias}.rid", $user_roles, 'IN'));
-    }
+    }*/
   }
 
   // Execute the queries.
@@ -510,10 +518,10 @@ function _avatar_selection_image_list($u
     $avs_image = $avatar->avatar;
     $name = $avatar->name;
     if (module_exists('image') && $imagecache_preset) {
-      $avatars[$avs_image] = theme('image_style', array('style_name' => $imagecache_preset, 'path' => $dir . '/' . $avs_image, 'alt' => $name, 'title' => $name));
+      $avatars[$avatar->fid] = theme('image_style', array('style_name' => $imagecache_preset, 'path' =>  'avatar_selection/' . $avs_image, 'alt' => $name, 'title' => $name));
     }
     else {
-      $avatars[$avs_image] = theme('image', array('path' => $url . '/' . $avs_image, 'width' => $name, 'height' => $name, 'alt' => NULL, 'title' => FALSE));
+      $avatars[$avatar->fid] = theme('image', array('path' => $url . '/' . $avs_image, 'width' => $name, 'height' => $name, 'alt' => NULL, 'title' => FALSE));
     }
   }
 
