--- group/advanced_assign.php
+++ group/advanced_assign.php
@@ -29,13 +29,9 @@ require_login($course);
 $PAGE->set_url($CFG->wwwroot.'/group/assign_advanced.php', array('courseid'=>$courseid));
 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
 
-//$PAGE->requires->yui2_lib('yahoo');
-//$PAGE->requires->yui2_lib('dom');
-//$PAGE->requires->yui2_lib('utilities');
-$PAGE->requires->js('/group/advanced_assign.js');
-$PAGE->requires->js('/lib/yui/2.8.2/build/dom/dom.js');
-$PAGE->requires->js('/lib/yui/2.8.2/build/yahoo/yahoo.js');
-$PAGE->requires->js('/lib/yui/2.8.2/build/utilities/utilities.js');
+$PAGE->requires->yui2_lib('yahoo');
+$PAGE->requires->yui2_lib('dom');
+$PAGE->requires->yui2_lib('utilities');
 
 $context = get_context_instance(CONTEXT_COURSE, $course->id);
 if (!has_capability('moodle/course:managegroups', $context)) {
@@ -107,13 +103,11 @@ if (!empty($update)) {
                 $groupmember->groupid = $group->id;
                 $groupmember->userid = $user->id;
 
-                echo 'insert';
-                $DB->insert_record('groups_members', array('groupmember' => $groupmember));
+                $DB->insert_record('groups_members', $groupmember);
 
             } elseif (!isset($updatedgroupsusers[$group->id][$user->id]) && isset($groupsusers[$group->id][$user->id])) {
             	// delete
-                echo 'delete'; // javascript needs fixing to make this work
-//                $DB->delete_records('groups_members', array('groupid' => $group->id, 'userid' => $user->id));
+                $DB->delete_records('groups_members', array('groupid' => $group->id, 'userid' => $user->id));
             }
             // anything else gets ignored.
         }
@@ -121,18 +115,19 @@ if (!empty($update)) {
     redirect($returnurl);
 }
 
-$PAGE->requires->js_init_call('init_advanced_assign');
-
 $strheading = get_string('editgroupsettings', 'group');
 
 $PAGE->navbar->add($strparticipants, new moodle_url('/user/index.php', array('id'=>$courseid)));
 $PAGE->navbar->add($strgroups, new moodle_url('/group/index.php', array('id'=>$courseid)));
 $PAGE->navbar->add($strheading);
+$PAGE->requires->js('/group/advanced_assign.js');
+$PAGE->requires->js_init_call('init_advanced_assign');
 
 /// Print header
 $PAGE->set_title($strgroups);
 $PAGE->set_heading($course->fullname . ': '.$strgroups);
 echo $OUTPUT->header();
+echo $OUTPUT->heading(get_string('adduserstogroup', 'group'), 3);
 
 // define column headers
 $grp_user_cnt = array();
@@ -142,6 +137,7 @@ $tableheaders = array(get_string('fullname'));
 foreach ($groups as $group) {
 	$tablecolumns[] = 'group-'.$group->id;
     $tableheaders[] = $group->name;
     $grp_user_cnt[$group->id] = 0;
 }
 $tablecolumns[] = 'checksum';
@@ -157,6 +153,23 @@ $table->set_attribute('class', 'generaltable generalbox');
 
 $table->setup();
 
+// Print form
+foreach ($roles as $role) {
+    $selected = $role->id == $roleid ? 'selected="selected"':'';
+}
+
+echo '<form id="advanced-assign-group-select-form" class="mform">';
+echo '  <input type="hidden" value="'.$courseid.'" name="courseid"/>';
+echo "<label for=\"groupselect\"> $strselectfromrole </label>";
+echo '<select id="roleselect" name="roleid" onchange="this.parentNode.submit();">';
+echo '    <option value="">'.get_string('all').'</option>';
+echo "<option value=\"{$role->id}\" $selected>".format_string($role->name)."</option>\n";
+echo '</select>';
+echo '</form>';
+
+echo '<form id="advanced-assign-form" class="mform" method="post" >';
+echo '<fieldset id="autogroup" class="clearfix">';
+echo '<legend class="ftoggler">'.$stradvancedadduserstogroups.'</legend>';
 
 foreach ($users as $user) {
 	$data = array();
@@ -183,29 +196,12 @@ foreach ($groups as $group) {
     $data[] = "<strong><span id=\"grp_user_cnt_{$group->id}\">{$grp_user_cnt[$group->id]}</span></strong>";
 }
 $table->add_data($data);
+$table->finish_output();
 
-// Print the dialogue
-echo '<form id="advanced-assign-group-select-form" class="mform">';
-echo '  <input type="hidden" value="'.$courseid.'" name="courseid"/>';
-echo "<label for=\"groupselect\"> $strselectfromrole </label>";
-echo '<select id="roleselect" name="roleid" onchange="this.parentNode.submit();">';
-echo '    <option value="">'.get_string('all').'</option>';
-foreach ($roles as $role) {
-    $selected = $role->id == $roleid ? 'selected="selected"':'';
-    echo "<option value=\"{$role->id}\" $selected>".format_string($role->name)."</option>\n";
-}
-
-echo '</select>';
-echo '</form>';
-
-
-echo '<form id="advanced-assign-form" class="mform" method="post" >';
+// Print the dialog
+echo '</fieldset>';
 echo '  <input type="hidden" value="'.$courseid.'" name="courseid"/>';
 echo '  <input type="hidden" value="1" name="update"/>';
-echo '<fieldset id="autogroup" class="clearfix">';
-echo '<legend class="ftoggler">'.$stradvancedadduserstogroups.'</legend>';
-$table->print_html();
-echo '</fieldset>';
 echo '<div class="buttons">';
 echo '  <input id="id_submitbutton" type="submit" value="Submit" name="submitbutton"/>';
 echo '  <input id="id_cancel" type="submit" value="Cancel" name="cancel"/>';
