--- multiselect.module.old	2009-04-12 13:28:38.000000000 -0300
+++ multiselect.module	2009-04-12 13:29:50.000000000 -0300
@@ -145,10 +145,6 @@
   // Get a list of all options for this field.
   $options = optionwidgets_options($field);
 
-  // Create some arrays for use later in the function.
-  $unselected_options = array();
-  $selected_options = array();
-
   // Insert Javascript and CSS for this widget.
   $path = drupal_get_path('module', 'multiselect');
   drupal_add_js($path .'/multiselect.js');
@@ -158,19 +154,23 @@
     //drupal_set_message('element = '.print_r($element));
     //drupal_set_message('options = '.print_r($options));
   }
-  // For each node, check if it's already selected. If it is, add it to the selected list
-  foreach ($options as $key => $value) {
-    if ($field['type'] == 'nodereference' && is_array($element['#value']['nid']) && in_array($key, $element['#value']['nid'])) { // Node reference fields
-      $selected_options[$key] = $value;
-    }
-    elseif ($field['type'] == 'userreference' && is_array($element['#value']['uid']) && in_array($key, $element['#value']['uid'])) { // User reference fields
-      $selected_options[$key] = $value;
-    }
-    elseif (is_array($element['#value']['value']) && in_array($key, $element['#value']['value'])) { // Other fields
-      $selected_options[$key] = $value;
+
+  // Create some arrays for use later in the function.
+  $unselected_options = array();
+  $selected_options = array();
+
+  // Add selected items to the array first
+  foreach ($element['#value'][$field_key] as $key) {
+    if (isset($options[$key])) {
+      $selected_options[$key] = $options[$key];
     }
-    else {
+  }
+  
+  // Add the remaining options to the arrays
+  foreach ($options as $key => $value) {
+    if (!isset($selected_options[$key])) {
       $unselected_options[$key] = $value;
+      $selected_options[$key] = $value;
     }
   }
 
@@ -201,7 +201,7 @@
     '#description' => $element['#description'],
     '#required' => isset($element['#required']) ? $element['#required'] : $field['required'],
     '#multiple' => isset($element['#multiple']) ? $element['#multiple'] : $field['multiple'],
-    '#options' => $options,
+    '#options' => $selected_options,
     '#size' => 10,
     '#prefix' => $prefix_pre . $prefix_options . $prefix_post,
     '#suffix' => "\n</div>\n",
