diff --git a/includes/plugins.inc b/includes/plugins.inc
index 771a9d7..089d901 100644
--- a/includes/plugins.inc
+++ b/includes/plugins.inc
@@ -129,10 +129,7 @@ class panels_cache_object {
     $this->head = drupal_add_html_head();
     $this->css = drupal_add_css();
     $this->tokens = ctools_set_page_token();
-
-    foreach (array('header', 'footer') as $scope) {
-      $this->js[$scope] = drupal_add_js(NULL, NULL, $scope);
-    }
+    $this->js = drupal_add_js();
   }
 
   /**
@@ -167,38 +164,24 @@ class panels_cache_object {
 
     foreach ($css as $name => $data) {
       if (!isset($start[$name])) {
-        $this->css[$name] = array($data['data'], $data['type'], $data['media'], $data['preprocess']);
+        $this->css[$name] = $data;
       }
     }
 
-    $js = array();
-    // A little less simple for js
-    foreach (array('header', 'footer') as $scope) {
-      $js[$scope] = drupal_add_js(NULL, NULL, $scope);
-    }
+    $js = drupal_add_js();
 
     $start = $this->js;
     $this->js = array();
 
-    foreach ($js as $scope => $scopes) {
-      foreach ($scopes as $type => $types) {
-        foreach ($types as $id => $info) {
-          if (!isset($start[$scope][$type][$id])) {
-            switch ($type) {
-              case 'setting':
-                $this->js[] = array($info, $type, $scope);
-                break;
-
-              case 'inline':
-                $this->js[] = array($info['code'], $type, $scope, $info['defer']);
-                break;
-
-              default:
-                $this->js[] = array($id, $type, $scope, $info['defer'], $info['cache']);
-            }
-          }
-        }
-      }
+    // If there are any differences between the old and the new javascript then
+    // store them to be added later.
+    if ($diff = array_diff_assoc($js, $start)) {
+      $this->js = $diff;
+    }
+
+    // Special case the settings key and get the difference of the data.
+    if ($settings_diff = array_diff_assoc($js['settings']['data'], $start['settings']['data'])) {
+      $this->js['settings'] = $settings_diff;
     }
 
     // And for tokens:
@@ -221,12 +204,19 @@ class panels_cache_object {
     }
     if (!empty($this->css)) {
       foreach ($this->css as $args) {
-        call_user_func_array('drupal_add_css', $args);
+        drupal_add_css($args['data'], $args);
       }
     }
     if (!empty($this->js)) {
-      foreach ($this->js as $args) {
-        call_user_func_array('drupal_add_js', $args);
+      foreach ($this->js as $key => $args) {
+        if ($key != 'settings') {
+          drupal_add_js($args['data'], $args);
+        }
+        else {
+          foreach ($args as $setting) {
+            drupal_add_js($setting, 'setting');
+          }
+        }
       }
     }
 
diff --git a/plugins/display_renderers/panels_renderer_standard.class.php b/plugins/display_renderers/panels_renderer_standard.class.php
index a37abe2..72847b2 100644
--- a/plugins/display_renderers/panels_renderer_standard.class.php
+++ b/plugins/display_renderers/panels_renderer_standard.class.php
@@ -529,14 +529,16 @@ class panels_renderer_standard {
       $content = $cache->content;
     }
     else {
+      if ($caching) {
+        $cache = new panels_cache_object();
+      }
       $content = ctools_content_render($pane->type, $pane->subtype, $pane->configuration, array(), $this->display->args, $this->display->context);
 
       foreach (module_implements('panels_pane_content_alter') as $module) {
         $function = $module . '_panels_pane_content_alter';
         $function($content, $pane, $this->display->args, $this->display->context);
       }
-      if ($caching) {
-        $cache = new panels_cache_object();
+      if ($caching && isset($cache)) {
         $cache->set_content($content);
         panels_set_cached_content($cache, $this->display, $this->display->args, $this->display->context, $pane);
         $content = $cache->content;
