--- unique_avatar.module.orig	2009-01-05 19:57:27.000000000 -0300
+++ unique_avatar.module	2009-01-05 22:06:38.000000000 -0300
@@ -19,15 +19,22 @@ function unique_avatar_user($type, &$edi
       $info = image_get_info($file->filepath);
       
       //Rename picture with unique id
-      $dest = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . '-' . md5(time()) . '.' . $info['extension'];
+      $picture_path=variable_get('user_picture_path','pictures');
+      $dest = $picture_path .'/picture-'. $user->uid . '-' . md5(time()) . '.' . $info['extension'];
+      
       if (file_move($file->filepath, $dest)) {
+        $full_picture_dir_path = file_directory_path() . "/$picture_path";
         
-        //Clean up existing picture
-        if (module_exists('imagecache')) {
-          imagecache_image_flush($user->picture);
-        }
-        file_delete($user->picture);
+        $old_picture_in_pictures_dir = !strncmp($full_picture_dir_path, $user->picture, drupal_strlen($full_picture_dir_path));
         
+        if ($old_picture_in_pictures_dir) {
+          //Clean up existing picture if the old picture was in the pictures dir; otherwise leave alone
+          if (module_exists('imagecache')) {
+            imagecache_image_flush($user->picture);
+          }
+          file_delete($user->picture);
+        }
+
         //Assign new picture
         $edit['picture'] = file_directory_path() . '/' . $dest;
         $user->picture = file_directory_path() . '/' . $dest;
