diff --git a/auth/ldap/auth.php b/auth/ldap/auth.php
index 88bb10d..d7af194 100644
--- a/auth/ldap/auth.php
+++ b/auth/ldap/auth.php
@@ -620,6 +620,7 @@ class auth_plugin_ldap extends auth_plugin_base {
         $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
         $table->add_field('username', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
         $table->add_field('mnethostid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
+        $table->add_field('context', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
         $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
         $table->add_index('username', XMLDB_INDEX_UNIQUE, array('mnethostid', 'username'));
 
@@ -638,6 +639,8 @@ class auth_plugin_ldap extends auth_plugin_base {
               array_push($contexts, $this->config->create_context);
         }
 
+        print "LDAP search filter: '" . $filter . "'\n";
+        print "LDAP attributes to get: '" . $this->config->user_attribute . "'\n";
         $fresult = array();
         foreach ($contexts as $context) {
             $context = trim($context);
@@ -663,8 +666,10 @@ class auth_plugin_ldap extends auth_plugin_base {
             if ($entry = @ldap_first_entry($ldapconnection, $ldap_result)) {
                 do {
                     $value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
+                    print "\n\tValue retrieved from LDAP: '" . $value[0] . "'\n";
                     $value = textlib::convert($value[0], $this->config->ldapencoding, 'utf-8');
-                    $this->ldap_bulk_insert($value);
+                    print "\tValue converted to: '" . $value . "'\n";
+                    $this->ldap_bulk_insert($value, $context);
                 } while ($entry = ldap_next_entry($ldapconnection, $entry));
             }
             unset($ldap_result); // free mem
@@ -813,7 +818,7 @@ class auth_plugin_ldap extends auth_plugin_base {
         // Find users missing in DB that are in LDAP
         // and gives me a nifty object I don't want.
         // note: we do not care about deleted accounts anymore, this feature was replaced by suspending to nologin auth plugin
-        $sql = 'SELECT e.id, e.username
+        $sql = 'SELECT e.id, e.username, e.context
                   FROM {tmp_extuser} e
                   LEFT JOIN {user} u ON (e.username = u.username AND e.mnethostid = u.mnethostid)
                  WHERE u.id IS NULL';
@@ -832,6 +837,8 @@ class auth_plugin_ldap extends auth_plugin_base {
 
             $transaction = $DB->start_delegated_transaction();
             foreach ($add_users as $user) {
+                print "\tSearching for user: '" . $user->username . "', from context: '" . $user->context;
+                if ($this->config->search_sub) { print "' (or one of its subcontexts)\n"; } else { "'\n"; }
                 $user = $this->get_userinfo_asobj($user->username);
 
                 // Prep a few params
@@ -927,11 +934,15 @@ class auth_plugin_ldap extends auth_plugin_base {
     /**
      * Bulk insert in SQL's temp table
      */
-    function ldap_bulk_insert($username) {
+    function ldap_bulk_insert($username, $context) {
+
         global $DB, $CFG;
 
+        print "\tUsername before lowercasing: '" . $username . "'\n";
         $username = textlib::strtolower($username); // usernames are __always__ lowercase.
+        print "\tUsername after lowercasing: '" . $username . "'\n";
         $DB->insert_record_raw('tmp_extuser', array('username'=>$username,
+                                                    'context'=>$context,
                                                     'mnethostid'=>$CFG->mnet_localhost_id), false, true);
         echo '.';
     }
