diff --git a/grade/import/csv/index.php b/grade/import/csv/index.php
index 886bf43..62283c1 100644
--- a/grade/import/csv/index.php
+++ b/grade/import/csv/index.php
@@ -216,6 +216,25 @@ if ($formdata = $mform->get_data()) {
     @set_time_limit(0);
     raise_memory_limit(MEMORY_EXTRA);
 
+    $user_fields = array(
+        'userid' => array(
+            'field' => 'id',
+            'label' => 'id',
+        ),
+        'useridnumber' => array(
+            'field' => 'idnumber',
+            'label' => 'idnumber',
+        ),
+        'useremail' => array(
+            'field' => 'email',
+            'label' => 'email address',
+        ),
+        'username' => array(
+            'field' => 'username',
+            'label' => 'username',
+        ),
+    );
+
     // we only operate if file is readable
     if ($fp = fopen($filename, "r")) {
 
@@ -264,39 +283,19 @@ if ($formdata = $mform->get_data()) {
                 }
 
                 switch ($t0) {
-                    case 'userid': //
-                        if (!$user = $DB->get_record('user', array('id' => $value))) {
-                            // user not found, abort whole import
-                            import_cleanup($importcode);
-                            echo $OUTPUT->notification("user mapping error, could not find user with id \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $value;
-                    break;
+                    case 'userid':
                     case 'useridnumber':
-                        if (!$user = $DB->get_record('user', array('idnumber' => $value))) {
-                             // user not found, abort whole import
-                            import_cleanup($importcode);
-                            echo $OUTPUT->notification("user mapping error, could not find user with idnumber \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $user->id;
-                    break;
                     case 'useremail':
-                        if (!$user = $DB->get_record('user', array('email' => $value))) {
-                            import_cleanup($importcode);
-                            echo $OUTPUT->notification("user mapping error, could not find user with email address \"$value\"");
-                            $status = false;
-                            break 3;
-                        }
-                        $studentid = $user->id;
-                    break;
                     case 'username':
-                        if (!$user = $DB->get_record('user', array('username' => $value))) {
+                        // Skip invalid row with blank user field
+                        if ($value === '') {
+                            continue 2;
+                        }
+
+                        if (!$user = $DB->get_record('user', array($user_fields[$t0]['field'] => $value))) {
+                            // user not found, abort whole import
                             import_cleanup($importcode);
-                            echo $OUTPUT->notification("user mapping error, could not find user with username \"$value\"");
+                            echo $OUTPUT->notification("user mapping error, could not find user with {$user_fields[$t0]['label']} \"$value\"");
                             $status = false;
                             break 3;
                         }
@@ -475,7 +474,12 @@ if ($formdata = $mform->get_data()) {
 
         /// at this stage if things are all ok, we commit the changes from temp table
         if ($status) {
-            grade_import_commit($course->id, $importcode);
+            ob_start();
+            if (grade_import_commit($course->id, $importcode)) {
+                ob_end_flush();
+            } else {
+                error('Grade Import failed during commit. Details:<br />' . ob_get_clean());
+            }
         }
         // temporary file can go now
         fclose($fp);
diff --git a/grade/import/lib.php b/grade/import/lib.php
index 3b1c7b0..7276b0b 100644
--- a/grade/import/lib.php
+++ b/grade/import/lib.php
@@ -45,6 +45,7 @@ function get_new_importcode() {
 function grade_import_commit($courseid, $importcode, $importfeedback=true, $verbose=true) {
     global $CFG, $USER, $DB, $OUTPUT;
 
+    $failed = false;
     $commitstart = time(); // start time in case we need to roll back
     $newitemids = array(); // array to hold new grade_item ids from grade_import_newitem table, mapping array
 
@@ -57,11 +58,10 @@ function grade_import_commit($courseid, $importcode, $importfeedback=true, $verb
         // instances of the new grade_items created, cached
         // in case grade_update fails, so that we can remove them
         $instances = array();
-        $failed = false;
         foreach ($newitems as $newitem) {
             // get all grades with this item
 
-            if ($grades = $DB->get_records('grade_import_values', array('newgradeitem' => $newitem->id))) {
+            if ($grades = $DB->get_records('grade_import_values', array('newgradeitem' => $newitem->id, 'importcode' => $importcode, 'importer' => $USER->id))) {
                 /// create a new grade item for this - must use false as second param!
                 /// TODO: we need some bounds here too
                 $gradeitem = new grade_item(array('courseid'=>$courseid, 'itemtype'=>'manual', 'itemname'=>$newitem->itemname), false);
@@ -104,7 +104,7 @@ function grade_import_commit($courseid, $importcode, $importfeedback=true, $verb
                 return false;
             }
             // get all grades with this item
-            if ($grades = $DB->get_records('grade_import_values', array('itemid' => $itemid))) {
+            if ($grades = $DB->get_records('grade_import_values', array('itemid' => $itemid, 'importcode' => $importcode, 'importer' => $USER->id))) {
 
                 // make the grades array for update_grade
                 foreach ($grades as $grade) {
@@ -112,19 +112,19 @@ function grade_import_commit($courseid, $importcode, $importfeedback=true, $verb
                         $grade->feedback = false; // ignore it
                     }
                     if (!$gradeitem->update_final_grade($grade->userid, $grade->finalgrade, 'import', $grade->feedback)) {
-                        $failed = 1;
-                        break 2;
+                        echo "Error setting grade for \"{$gradeitem->itemname}\" for userid {$grade->userid}<br />" . PHP_EOL;
+                        $failed = true;
                     }
                 }
                 //$itemdetails -> idnumber = $gradeitem->idnumber;
                 $modifieditems[] = $itemid;
 
             }
+        }
 
-            if (!empty($failed)) {
-                import_cleanup($importcode);
-                return false;
-            }
+        if ($failed) {
+            import_cleanup($importcode);
+            return false;
         }
     }
 
