diff --git a/domain.api.php b/domain.api.php
index b832e92..e292923 100644
--- a/domain.api.php
+++ b/domain.api.php
@@ -61,13 +61,25 @@ function hook_domainload(&$domain) {
 function hook_domainupdate($op, $domain, $form_values = array()) {
   switch ($op) {
     case 'create':
-      db_query("INSERT INTO {mytable} (subdomain, sitename) VALUES ('%s', '%s')", $domain['subdomain'], $domain['sitename']);
+      db_insert('mytable')
+        ->fields(array(
+          'domain_id' => $domain['domain_id'],
+          'myvar' => 1,
+        ))
+        ->execute();
       break;
     case 'update':
-      db_query("UPDATE {mytable} SET subdomain = '%s', sitename = '%s' WHERE domain_id = %d", $domain['subdomain'], $domain['sitename'], $domain['domain_id']);
+      db_update('mytable')
+        ->fields(array(
+          'status' => 1,
+        ))
+        ->condition('domain_id', $domain['domain_id'])
+        ->execute();
       break;
     case 'delete':
-      db_query("DELETE FROM {mytable} WHERE subdomain = '%s'", $domain['subdomain']);
+      db_delete('mytable')
+        ->condition('domain_id', $domain['domain_id'])
+        ->execute();
       break;
   }
 }
@@ -154,10 +166,21 @@ function hook_domainnav($domain) {
  */
 function hook_domaincron($domain) {
   // Run a node query.
-  $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n ORDER BY n.changed"), 0, 1);
-  $node = db_fetch_object($result);
-  // Set a variable for each domain containing the last node updated.
-  variable_set('domain_' . $domain['domain_id'] . '_lastnode', $node->nid);
+  $select = db_select('node', 'n')
+    ->fields('n', array('nid'))
+    ->condition('status', 1)
+    ->orderBy('created', 'DESC')
+    ->extend('PagerDefault')
+    ->limit(1);
+  // Note that we don't tag this query with 'node_access' because it is an
+  // administrative query and we want to return all nodes for a specific domain.
+  $select->join('domain_access', 'da', 'n.nid = da.nid AND da.domain_id = :domain_id',
+    array(':domain_id' => $domain['domain_id']));
+  $nid = $select->execute()->fetchCol();
+  if ($node = node_load($nid)) {
+    // Set a variable for each domain containing the last node updated.
+    variable_set('domain_' . $domain['domain_id'] . '_lastnode', $node->nid);
+  }
 }
 
 /**
@@ -600,7 +623,7 @@ function hook_domain_bootstrap_full($domain) {
  * @param $options
  *   The path options.
  * @param $original_path
- *   The raw path request from the URL. 
+ *   The raw path request from the URL.
  *
  * @ingroup domain_hooks
  */
diff --git a/domain_theme/domain_theme.module b/domain_theme/domain_theme.module
index 166a912..85adc33 100644
--- a/domain_theme/domain_theme.module
+++ b/domain_theme/domain_theme.module
@@ -398,3 +398,14 @@ function domain_theme_form_alter(&$form, &$form_state, $form_id) {
   }
   $form['domain_color_defaults'] = array('#type' => 'value', '#value' => $color_settings);
 }
+
+/**
+ * Implements hook_domainupdate().
+ */
+function domain_theme_domainupdate($op, $domain, $form_values = array()) {
+  if ($op == 'delete') {
+    db_delete('domain_theme')
+      ->condition('domain_id', $domain['domain_id'])
+      ->execute();
+  }
+}
