diff --git a/domain.module b/domain.module
index 2d49d23..82572ff 100644
--- a/domain.module
+++ b/domain.module
@@ -970,6 +970,9 @@ function domain_save($values, $form_values = array()) {
   // Lookup the modified record and return it.
   $domain = domain_lookup(NULL, $values['subdomain'], TRUE);
 
+  // Clear static caches.
+  domain_static_reset();
+
   // In all cases, we need to force a menu rebuild, which also clears the cache.
   menu_rebuild();
 
@@ -977,6 +980,15 @@ function domain_save($values, $form_values = array()) {
 }
 
 /**
+ * Clear static caches used for domain listings.
+ */
+function domain_static_reset() {
+  drupal_static_reset('domain_id_list');
+  drupal_static_reset('domain_domains');
+  drupal_static_reset('domain_list_by_machine_name');
+}
+
+/**
  * Delete a domain record.
  *
  * @param $domain
diff --git a/domain_source/domain_source.domain.inc b/domain_source/domain_source.domain.inc
index e680290..ca35d28 100644
--- a/domain_source/domain_source.domain.inc
+++ b/domain_source/domain_source.domain.inc
@@ -12,9 +12,14 @@
  */
 function domain_source_domain_source_alter(&$source, $nid) {
   $source_id = domain_source_lookup($nid);
+  // If FALSE returned, no source is defined.
+  if (!$source_id) {
+    return;
+  }
+  // The source_id always returns a valid domain.
   $source = domain_lookup($source_id);
   // DOMAIN_SOURCE_USE_ACTIVE is the status for 'Use active domain.'
-  if ($source == -1 || $source == DOMAIN_SOURCE_USE_ACTIVE) {
+  if ($source == DOMAIN_SOURCE_USE_ACTIVE) {
     $source = domain_get_domain();
   }
 }
diff --git a/domain_source/domain_source.info b/domain_source/domain_source.info
index 752ac76..79313a8 100644
--- a/domain_source/domain_source.info
+++ b/domain_source/domain_source.info
@@ -8,3 +8,4 @@ files[] = domain_source.install
 files[] = domain_source.module
 files[] = domain_source.views.inc
 files[] = includes/domain_source_handler_filter_domain_id.inc
+files[] = tests/domain_source.test
diff --git a/domain_source/domain_source.module b/domain_source/domain_source.module
index 4ec6298..ef37624 100644
--- a/domain_source/domain_source.module
+++ b/domain_source/domain_source.module
@@ -31,7 +31,7 @@ function domain_source_form_alter(&$form, &$form_state, $form_id) {
   }
   if (!isset($default_source)) {
     $source = domain_load_domain_id(variable_get('domain_source_' . $form['#node']->type, DOMAIN_SOURCE_USE_ACTIVE));
-    if ($source < 0) {
+    if ($source <= 0) {
       $default_source = DOMAIN_SOURCE_USE_ACTIVE;
     }
     else {
@@ -188,13 +188,17 @@ function domain_source_node_validate($node, $form) {
  * Implements hook_node_load().
  */
 function domain_source_node_load($nodes, $types) {
-  $source = domain_source_lookup($nodes);
-  foreach ($nodes as $node) {
+  $results = db_query("SELECT nid, domain_id FROM {domain_source} WHERE nid IN (:nid)",
+    array(':nid' => array_keys($nodes)))->fetchAllAssoc('nid');
+
+  foreach ($nodes as $nid => $node) {
     // Cannot load if the node has not been created yet.
-    if (!isset($node->nid)) {
-      continue;
+    if (!isset($results[$nid])) {
+      $nodes[$node->nid]->domain_source = NULL;
+    }
+    else {
+      $nodes[$node->nid]->domain_source = $results[$node->nid]->domain_id;
     }
-    $nodes[$node->nid]->domain_source = $source[$node->nid];
   }
 }
 
@@ -213,8 +217,6 @@ function domain_source_node_insert($node) {
       'domain_id' => $node->domain_source,
     ))
     ->execute();
-  // Clear the cache.
-  domain_source_lookup($node->nid, array(), NULL, TRUE);
 }
 
 /**
@@ -239,122 +241,42 @@ function domain_source_node_delete($node) {
  * Display debugging information for a node.
  */
 function domain_source_node_view($node, $view_mode) {
-  if (empty($node->nid) || !in_array($view_mode, array('full', 'teaser'))) {
+  if (empty($node->nid) || !in_array($view_mode, array('full', 'teaser')) || !variable_get('domain_debug', 0) || !user_access('set domain access')) {
     return;
   }
-  if (variable_get('domain_debug', 0) && user_access('set domain access') && isset($node->domain_source)) {
-    $source = domain_get_node_match($node->nid);
-    $extra = ' ';
-    $use_active = db_query("SELECT domain_id FROM {domain_source} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
-    if ($use_active === FALSE) {
-      $extra .= t('(determined automatically)');
-    }
-    elseif ($use_active == DOMAIN_SOURCE_USE_ACTIVE) {
-      $extra .= t('(using active domain)');
-    }
-    if (!isset($node->content['domain']['#markup'])) {
-      $node->content['domain']['#markup'] = '';
-    }
-    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source', array('%source' => $source['sitename'])) . $extra . '</p>';
+  $_domain = domain_get_domain();
+  $source = domain_get_node_match($node->nid);
+  if (is_null($node->domain_source)) {
+    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source (determined automatically)', array('%source' => $source['sitename'])) . '</p>';
+  }
+  elseif ($node->domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
+    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source (using active domain)', array('%source' => $_domain['sitename'])) . '</p>';
+  }
+  else {
+    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source', array('%source' => $source['sitename'])) . '</p>';
   }
 }
 
 /**
  * Get the source domains for multiple matches, mimicking node_load().
  *
- * @param $nodes
- *   An array of nodes, keyed by node id, or a single node id.
- * @param $domains
- *   An optional array of domain ids; can only be used for a single node.
- * @param $domain_site
- *   Optional flag indicating that the node is assigned to all domains.
- * @param $reset
- *   Force a reset of the static data lookup.
+ * @param $nid
+ *  A node id.
+ *
  * @return
- *   An associative array of data, keyed by node id, or a single array.
- *   The data array is the active source domain to use, as returned by domain_lookup().
+ *  The domain_id of the canonical source domain or FALSE if not set.
  */
-function domain_source_lookup($nodes, $domains = array(), $domain_site = FALSE, $reset = FALSE) {
+function domain_source_lookup($nid) {
   $_domain = domain_get_domain();
-  $lookups = &drupal_static(__FUNCTION__);
-  // Reset?
-  if ($reset) {
-    $lookups = array();
-  }
-  // Ensure we format the lookup correctly.
-  if (!is_array($nodes)) {
-    $nid = $nodes;
-    $node_ids = array($nid);
-    $array = FALSE;
-  }
-  else {
-    $node_ids = array_keys($nodes);
-    $array = TRUE;
-  }
-  // Static cache.
-  if (!$reset && isset($nid) && isset($lookups[$nid])) {
-    return $lookups[$nid];
-  }
-
-  $result = db_query("SELECT nid, domain_id FROM {domain_source} WHERE nid IN (:nid)", array(':nid' => $node_ids))->fetchAll();
-
-  foreach ($result as $data) {
-    $source = $data->domain_id;
-
-    // When called from url_rewrite_outbound(), we might have no data,
-    // but if $nodes is populated, then we have run domain_node_load().
-    if (is_array($nodes) && isset($nodes[$data->nid]->domains)) {
-      $domains = $nodes[$data->nid]->domains;
-      $domain_site = $nodes[$data->nid]->domain_site;
-    }
-    if (empty($domains)) {
-      $lookup = domain_get_node_domains($data->nid);
-      $domain_site = $lookup['domain_site'];
-      $domains = $lookup['domain_id'];
-    }
-    // If no valid source is found, take the first match from the $domains array.
-    if (empty($domain_site) && !empty($domains) && !in_array($source, $domains)) {
-      $source = current($domains);
-    }
-    // If set to 'Use active domain', return that status flag.
-    if ($source == DOMAIN_SOURCE_USE_ACTIVE) {
-      $lookups[$data->nid] = $source;
-    }
-    // Otherwise, lookup the source domain.
-    else {
-      $domain = domain_lookup($source);
-      if (isset($domain['domain_id'])) {
-        $lookups[$data->nid] = $domain['domain_id'];
-      }
-    }
-  }
-  // Make sure we found something for every requested node.
-  $no_records = $node_ids;
-  if (!empty($lookups)) {
-    $no_records = array_diff($node_ids, array_keys($lookups));
-  }
-  foreach ($no_records as $nid) {
-    $type = '';
-    if (isset($nodes[$nid]->type)) {
-      $type = $nodes[$nid]->type;
-    }
-    elseif ($node = node_load($nid)) {
-      $type = $node->type;
-    }
-    // Global setting for 'do not change domain.'
-    $default_source = domain_load_domain_id(variable_get('domain_source_' . $type, DOMAIN_SOURCE_USE_ACTIVE));
-    if ($default_source < 0 || empty($default_source)) {
-      $lookups[$nid] = $_domain['domain_id'];
-    }
-    else {
-      $default = domain_lookup($default_source);
-      $lookups[$nid] = $default['domain_id'];
-    }
+  $source = FALSE;
+  $node = node_load($nid);
+  if ($node->domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
+    $source = $node->domain_source;
   }
-  if ($array) {
-    return $lookups;
+  elseif ($domain = domain_load($node->domain_source)) {
+    $source = $domain['domain_id'];
   }
-  return $lookups[$nid];
+  return $source;
 }
 
 /**
@@ -389,7 +311,7 @@ function domain_source_validate($form, &$form_state) {
   if ($form_state['values']['operation'] == 'domain') {
     $default = domain_default_id();
     $key = $form_state['values']['domain_source'];
-    if ($form_state['values']['domain_site'] && $key == $default || $key == DOMAIN_SOURCE_USE_ACTIVE) {
+    if (!empty($form_state['values']['domain_site']) || $key == DOMAIN_SOURCE_USE_ACTIVE) {
       // These cases are acceptable, so we let them pass.
       // I find this code easier to read than a compound negative IF statement.
     }
@@ -397,8 +319,7 @@ function domain_source_validate($form, &$form_state) {
       // In this case, we need to check all the selected nodes, which is resource intensive.
       $nodes = array_filter($form_state['values']['nodes']);
       foreach ($nodes as $nid) {
-        $source = domain_source_lookup($nid, array(), $form_state['values']['domain_site'], TRUE);
-        $key = $source['domain_id'];
+        $key = domain_source_lookup($nid);
         // Test for the first two behaviors, which add domains to the node.
         $behavior = $form_state['values']['behavior'];
         if ($behavior < 2) {
diff --git a/domain_source/tests/domain_source.test b/domain_source/tests/domain_source.test
index e69de29..8be0304 100644
--- a/domain_source/tests/domain_source.test
+++ b/domain_source/tests/domain_source.test
@@ -0,0 +1,109 @@
+<?php
+
+/**
+ * @file
+ * Simpletest for Domain Source.
+ */
+
+class DomainSourceTest extends DomainTestCase {
+
+  public function setUp($list = array()) {
+    $modules = array_merge(array('domain_source'), $list);
+    parent::setUp($modules);
+    // Create some test domains.
+    $this->domainCreateDomains();
+  }
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Domain Source tests',
+      'description' => 'Test the functionality provided by Domain Source to assign canonical domains to content.',
+      'group' => 'Domain Source',
+    );
+  }
+
+  // Test that content is not initially assigned.
+  public function testDomainSourceInstall() {
+    // Check that nodes were created.
+    $node_count = db_query("SELECT COUNT(nid) FROM {node}")->fetchField();
+    $this->assertTrue($node_count == 10, t('Ten nodes were created on installation.'));
+    // Check that no source records appear.
+    $records = db_query("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {domain_source} ds ON n.nid = ds.nid")->fetchField();
+    $this->assertTrue(empty($records), t('Initial nodes have no domain source data.'));
+  }
+
+  // Test simple saving and loading of node data.
+  public function testDomainSourceSave() {
+    // These nodes should all be assigned to the primary domain.
+    $node = node_load(1);
+    // $node->domain_source should be NULL.
+    $this->assertTrue(is_null($node->domain_source), t('Source domain for initial content returns NULL.'));
+
+    // Save the node with an assigned source. Then load and check.
+    $node->domain_source = domain_default_id();
+    node_save($node);
+
+    // Check that a source record appears.
+    $records = db_query("SELECT COUNT(n.nid) FROM {node} n INNER JOIN {domain_source} ds ON n.nid = ds.nid")->fetchField();
+    $this->assertTrue($records == 1, t('Saved node has domain source data.'));
+
+    // Check that the data is loaded cleanly.
+    $node = node_load(1);
+    $this->assertTrue($node->domain_source == domain_default_id(), t('Source domain for initial content loaded correctly.'));
+
+    // Save the node with no assigned source. Then load and check.
+    $node->domain_source = DOMAIN_SOURCE_USE_ACTIVE;
+    node_save($node);
+
+    // Check that the data is loaded cleanly.
+    $node = node_load(1);
+    $this->assertTrue($node->domain_source == DOMAIN_SOURCE_USE_ACTIVE, t('Source domain for initial content loaded correctly as "use active domain".'));
+  }
+
+  // Test the node form.
+  public function testDomainSourceForm() {
+    // Test the node edit form.
+    $type = 'article';
+    $admin_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'set domain access'));
+    $this->drupalLogin($admin_account);
+    $form = $this->domainNodeForm($admin_account->uid, $type);
+    // Check the form element.
+    $this->assertTrue(isset($form['domain']['domain_source']), t('Domain Source node form element appears.'));
+    $domains = domain_domains();
+    // There should be four domains, plus the use current domain option.
+    $this->assertTrue(count($form['domain']['domain_source']['#options']) == (count($domains) + 1), t('Domain source options list correctly.'));
+    // The default domain option should be use active.
+    $this->assertTrue($form['domain']['domain_source']['#default_value'] == DOMAIN_SOURCE_USE_ACTIVE, t('The default form value is "use active".'));
+
+    // Set the default value for this node type and check again. Note that the
+    // variable is stored as a machine_name and transforms to an id by the form.
+    variable_set('domain_source_article', domain_default_machine_name());
+    $form = $this->domainNodeForm($admin_account->uid, $type);
+    // The default domain option should be default.
+    $this->assertTrue($form['domain']['domain_source']['#default_value'] == domain_default_id(), t('The default form value is the node type default.'));
+
+    // TODO: There are more advanced form use-cases. They can wait.
+
+  }
+
+  // Test source URL settings.
+  public function testDomainSourceDefaults() {
+    $_domain = domain_get_domain();
+    $node = node_load(1);
+    // By default, this node is not assigned, so it should be set to use
+    // the active domain.
+    $source = domain_source_lookup($node->nid);
+    $this->assertTrue($source === FALSE, t('Unassigned node defaults to current domain.'));
+
+    // Save the node with an assigned source. Then load and check.
+    $domain = domain_load(2);
+    $node->domain_source = $domain['domain_id'];
+    node_save($node);
+    $source = domain_source_lookup($node->nid);
+    $this->assertTrue($source == $domain['domain_id'], t('Assigned node set to proper domain.'));
+    $url = url('node/1');
+    $parts = parse_url($url);
+    $this->assertTrue($parts['host'] == $domain['subdomain'], t('URL rewrite returned correct domain.'));
+  }
+
+}
diff --git a/tests/domain.test b/tests/domain.test
index ef7e296..335f35c 100644
--- a/tests/domain.test
+++ b/tests/domain.test
@@ -95,12 +95,24 @@ class DomainTestCase extends DrupalWebTestCase {
 
   /**
    * Helper function for form introspection.
+   *
+   * Forms are sensitive to the global user account, so we must reset it when
+   * using this technique.
+   *
+   * @param $uid
+   *   A user id representing the account to test.
+   * @param $type
+   *   The node type of the form.
    */
   public function domainNodeForm($uid, $type) {
-    $user = user_load($uid);
+    $temp = $GLOBALS['user'];
+    $user = user_load($uid, TRUE);
+    $GLOBALS['user'] = $user;
     $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => LANGUAGE_NONE);
     module_load_include('inc', 'node', 'node.pages');
-    return drupal_get_form($type . '_node_form', $node);
+    $form = drupal_get_form($type . '_node_form', $node);
+    $GLOBALS['user'] = $temp;
+    return $form;
   }
 
   /**
