diff --git a/domain.module b/domain.module
index 5eec805..da4cbc8 100644
--- a/domain.module
+++ b/domain.module
@@ -76,7 +76,7 @@ function domain_api_version() {
  */
 function domain_init() {
   global $_domain, $conf;
- 
+
   if (!is_array($_domain)) {
     $_domain = array();
   }
@@ -1709,6 +1709,22 @@ function domain_node_presave($node) {
 }
 
 /**
+ * Implements hook_node_insert().
+ */
+function domain_node_insert($node) {
+  // Any function that tries to call domain_get_node_domains() or
+  // domain_get_node_match() needs this data.
+  domain_node_access_records($node);
+}
+
+/**
+ * Implements hook_node_update().
+ */
+function domain_node_update($node) {
+  domain_node_access_records($node);
+}
+
+/**
  * Get the best matching domain for a node link.
  *
  * @param $nid
@@ -1730,10 +1746,15 @@ function domain_get_node_match($nid) {
   $source = NULL;
   if ($id !== FALSE) {
     $source = domain_lookup($id);
-    drupal_alter('domain_source', $source, $nid);
-    $domain[$nid] = $source;
+  }
+  else {
+    $source = domain_get_domain();
   }
 
+  // Allow other modules to intervene.
+  drupal_alter('domain_source', $source, $nid);
+  $domain[$nid] = $source;
+
   return $source;
 }
 
@@ -1766,19 +1787,14 @@ function domain_get_path_match($path) {
  *
  * @param $nodes
  *   An array of nodes, keyed by node id, or a single node id.
- * @param $reset
- *   A boolean flag indicating the need to reset the static variable for the node.
  *
  * @return
  *   An array of data, keyed by node id, or a single array for a single node.
  *   The data array, consists of two parts. 'domain_id' is an array of active domain ids.
  *   'domain_site' is a TRUE/FALSE boolean indicating affiliate status.
  */
-function domain_get_node_domains($nodes, $reset = FALSE) {
-  $lookup = &drupal_static(__FUNCTION__);
-  if (!isset($lookup) || $reset) {
-    $lookup = array();
-  }
+function domain_get_node_domains($nodes) {
+  $lookup = &drupal_static(__FUNCTION__, array());
   // Ensure we form our data properly.
   if (!is_array($nodes)) {
     $node_ids[$nodes] = $nodes;
@@ -2005,8 +2021,9 @@ function _domain_store_grants($nid, $grants = array()) {
     $query->execute();
   }
 
-  // Reset the static lookup for this node.
-  domain_get_node_domains($nid, TRUE);
+  // Reset the node-related domain static variables.
+  drupal_static_reset('domain_get_node_domains');
+  drupal_static_reset('domain_get_node_match');
 
   // Ensure that our default grant is present.
   domain_set_default_grant();
diff --git a/domain.tokens.inc b/domain.tokens.inc
index 29a7397..80ad324 100644
--- a/domain.tokens.inc
+++ b/domain.tokens.inc
@@ -31,7 +31,7 @@ function domain_token_info() {
     'name' => t('Domain id'),
     'description' => t('The domain ID.'),
   );
-  $info['tokens']['domain']['machine_name'] = array(
+  $info['tokens']['domain']['machine-name'] = array(
     'name' => t('Domain machine name'),
     'description' => t('The domain machine identifier.'),
   );
@@ -71,22 +71,19 @@ function domain_token_info() {
 function domain_tokens($type, $tokens, array $data = array(), array $options = array()) {
   $sanitize = !empty($options['sanitize']);
   $replacements = array();
+
   // Base token handling.
-  if ($type == 'domain') {
-    // Get the active domain context.
-    if (!empty($data['domain'])) {
-      $domain = $data['domain'];
-    }
-    else {
-      $domain = domain_get_domain();
-    }
+  if ($type == 'domain' && !empty($data['domain'])) {
+    $domain = $data['domain'];
+
     // Loop through the tokens and replace the elements.
     foreach ($tokens as $name => $original) {
       switch ($name) {
         case 'id':
           $replacements[$original] = $domain['domain_id'];
           break;
-        case 'machine_name':
+        case 'machine-name':
+        case 'machine_name': // Renamed to 'machine-name'.
           $replacements[$original] = $domain['machine_name'];
           break;
         case 'name':
@@ -120,30 +117,31 @@ function domain_tokens($type, $tokens, array $data = array(), array $options = a
       }
     }
   }
+
   // Node tokens.
   if ($type == 'node' && !empty($data['node'])) {
-    $domain = domain_get_node_match($data['node']->nid);
     $node = $data['node'];
-    // Check for valid domain data.
     $domain = domain_get_node_match($node->nid);
-    if ($domain == -1) {
-      return;
+    if (empty($domain) || $domain == -1) {
+      return $replacements;
     }
     // Loop through the tokens to not waste cycles.
     foreach ($tokens as $name => $original) {
       if ($name == 'domain') {
-        $replacements[$original] = $sanitize ? check_plain($domain['subdomain']) : $domain['subdomain'];
+        $replacements[$original] = $sanitize ? check_plain($domain['sitename']) : $domain['sitename'];
       }
     }
     if ($domain_tokens = token_find_with_prefix($tokens, 'domain')) {
       $replacements += token_generate('domain', $domain_tokens, array('domain' => $domain), $options);
     }
   }
+
   // Current domain tokens.
   elseif ($type == 'current-domain') {
     $current_domain = domain_get_domain();
     $replacements += token_generate('domain', $tokens, array('domain' => $current_domain), $options);
   }
+
   // Default domain tokens.
   elseif ($type == 'default-domain') {
     $default_domain = domain_default(FALSE);
diff --git a/tests/domain.test b/tests/domain.test
index 3de0cf6..cdb0902 100644
--- a/tests/domain.test
+++ b/tests/domain.test
@@ -801,3 +801,133 @@ class DomainFilterTest extends DomainTestCase {
   }
 
 }
+
+class DomainTokenTest extends DomainTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Domain token tests',
+      'description' => 'Tests for domain-based tokens.',
+      'group' => 'Domain Access',
+    );
+  }
+
+  function setUp($list = array()) {
+    $list[] = 'domain_test';
+    parent::setUp($list);
+
+    // Create dummy domains.
+    $this->domains = $this->domainCreateDomains(array('one', 'two', 'three'), 'example.com');
+  }
+
+  /**
+   * Test the domain tokens.
+   */
+  function testDomainTokens() {
+    // Set the 'current' domain to the non-default domain 'three'.
+    domain_set_domain($this->domains['three']['domain_id']);
+
+    // Test the [domain:*] base tokens with a valid domain provided as data.
+    $tokens = array(
+      'id' => (string) $this->domains['one']['domain_id'],
+      'machine-name' => 'one_example_com',
+      'name' => 'one',
+      'path' => 'http://one.example.com/',
+      'url' => 'oneexamplecom',
+      'hostname' => 'one.example.com',
+      'subdomain' => 'one',
+    );
+    $data['domain'] = $this->domains['one'];
+    $replacements = $this->assertTokens('domain', $data, $tokens);
+
+    // Ensure that the [domain:*] tokens do not replace anything if not
+    // provided with a valid domain.
+    $tokens = array(
+      'id' => NULL,
+      'name' => NULL,
+    );
+    $replacements = $this->assertTokens('domain', array(), $tokens);
+
+    // Test the [current-domain:*] tokens.
+    $tokens = array(
+      'id' => (string) $this->domains['three']['domain_id'],
+      'machine-name' => 'three_example_com',
+      'name' => 'three',
+      'path' => 'http://three.example.com/',
+      'url' => 'threeexamplecom',
+      'hostname' => 'three.example.com',
+      'subdomain' => 'three',
+    );
+    $replacements = $this->assertTokens('current-domain', array(), $tokens);
+
+    // Test the [default-domain:*] tokens.
+    $tokens = array(
+      'id' => '1',
+      'name' => 'TestDomainSitename',
+    );
+    $replacements = $this->assertTokens('default-domain', array(), $tokens);
+  }
+
+  /**
+   * Test that the tokens perform as expected when generated from
+   * hook_node_insert() and hook_node_update().
+   *
+   * @see http://drupal.org/node/1336698
+   */
+  function testRegression1336698() {
+    $admin_account = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'set domain access'));
+    $this->drupalLogin($admin_account);
+
+    // Insert a new node using defaults and check the message generated by
+    // domain_test_node_insert() and token_replace().
+    $edit = array(
+      'title' => 'Test page 1',
+    );
+    $this->drupalPost('node/add/page', $edit, t('Save'));
+    $this->assertText('Inserted node Test page 1 assigned to domain 1 (TestDomainSitename).');
+
+    // Select a different domain to assign to this node at random.
+    shuffle($this->domains);
+    $domain = reset($this->domains);
+    $this->assertNotEqual($domain['sitename'], 'TestDomainSitename', 'Random domain selected.');
+
+    // Update the node assigning it to the new domain and check the message
+    // generated by domain_test_node_insert() and token_replace().
+    $node = $this->drupalGetNodeByTitle('Test page 1');
+    $edit = array(
+      'domains[1]' => FALSE, // Default domain is ID 1.
+      'domains[' . $domain['domain_id'] . ']' => TRUE,
+    );
+    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
+    $this->assertText("Updated node Test page 1 assigned to domain {$domain['domain_id']} ({$domain['sitename']}).");
+  }
+
+  /**
+   * Function copied from TokenTestHelper::assertTokens().
+   */
+  function assertTokens($type, array $data, array $tokens, array $options = array()) {
+    $input = $this->mapTokenNames($type, array_keys($tokens));
+    $replacements = token_generate($type, $input, $data, $options);
+    foreach ($tokens as $name => $expected) {
+      $token = $input[$name];
+      if (!isset($expected)) {
+        $this->assertTrue(!isset($values[$token]), t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
+      }
+      elseif (!isset($replacements[$token])) {
+        $this->fail(t("Token value for @token was not generated.", array('@type' => $type, '@token' => $token)));
+      }
+      else {
+        $this->assertIdentical($replacements[$token], $expected, t("Token value for @token was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $replacements[$token], '@expected' => $expected)));
+      }
+    }
+
+    return $replacements;
+  }
+
+  function mapTokenNames($type, array $tokens = array()) {
+    $return = array();
+    foreach ($tokens as $token) {
+      $return[$token] = "[$type:$token]";
+    }
+    return $return;
+  }
+}
diff --git a/tests/domain_test.module b/tests/domain_test.module
index cdc016c..935542c 100644
--- a/tests/domain_test.module
+++ b/tests/domain_test.module
@@ -164,3 +164,17 @@ function domain_test_domain_batch_alter(&$batch) {
   // Rename 'Put site into maintenance mode'.
   $batch['maintenance_mode']['#form']['#title'] = t('Test reset value');
 }
+
+/**
+ * Implements hook_node_insert().
+ */
+function domain_test_node_insert($node) {
+  drupal_set_message(token_replace('Inserted node [node:title] assigned to domain [node:domain:id] ([node:domain]).', array('node' => $node)));
+}
+
+/**
+ * Implements hook_node_update().
+ */
+function domain_test_node_update($node) {
+  drupal_set_message(token_replace('Updated node [node:title] assigned to domain [node:domain:id] ([node:domain]).', array('node' => $node)));
+}
