diff --git a/chartbeat.module b/chartbeat.module
index 2cbf882..036be71 100644
--- a/chartbeat.module
+++ b/chartbeat.module
@@ -33,45 +33,31 @@ function chartbeat_init() {
   $uid = variable_get('chartbeat_uid', '');
   if (!empty($uid) && is_numeric($uid)) {
     // Add the header script to start the timer.
-    drupal_add_html_head(array(
-      '#tag' => 'script',
-      '#attributes' => array('type' => 'text/javascript'),
-      '#value' => 'var _sf_startpt=(new Date()).getTime();',
-    ), 'chartbeat');
+    drupal_add_js('var _sf_startpt=(new Date()).getTime();', array('type' => 'inline', 'group' => JS_LIBRARY));
     // Inject the Chartbeat settings.
     drupal_add_js(array('chartbeat' => array(
       'uid' => (int)$uid,
       'domain' => variable_get('chartbeat_domain', ''),
     )), 'setting');
-  }
-}
-
-/**
- * Implements hook_page_alter().
- */
-function chartbeat_page_alter(&$page) {
-  $uid = variable_get('chartbeat_uid', '');
-  if (!empty($uid)) {
-    $output = <<<EOT
-<script type="text/javascript">
-  var _sf_async_config=Drupal.settings.chartbeat;
-  (function(){
-    function loadChartbeat() {
-      window._sf_endpt=(new Date()).getTime();
-      var e = document.createElement('script');
-      e.setAttribute('language', 'javascript');
-      e.setAttribute('type', 'text/javascript');
-      e.setAttribute('src',
-         (("https:" == document.location.protocol) ? "https://s3.amazonaws.com/" : "http://") +
-         "static.chartbeat.com/js/chartbeat.js");
-      document.body.appendChild(e);
-    }
-    var oldonload = window.onload;
-    window.onload = (typeof window.onload != 'function') ?
-       loadChartbeat : function() { oldonload(); loadChartbeat(); };
-  })();
-</script>
-EOT;
-    $page['page_bottom']['chartbeat'] = array('#markup' => $output);
+    // Add the lazyloader for the main script.
+    $js = <<<EOD
+      var _sf_async_config=Drupal.settings.chartbeat;
+      (function(){
+        function loadChartbeat() {
+          window._sf_endpt=(new Date()).getTime();
+          var e = document.createElement('script');
+          e.setAttribute('language', 'javascript');
+          e.setAttribute('type', 'text/javascript');
+          e.setAttribute('src',
+            (("https:" == document.location.protocol) ? "https://s3.amazonaws.com/" : "http://") +
+            "static.chartbeat.com/js/chartbeat.js");
+          document.body.appendChild(e);
+        }
+        var oldonload = window.onload;
+        window.onload = (typeof window.onload != 'function') ?
+          loadChartbeat : function() { oldonload(); loadChartbeat(); };
+      })();
+EOD;
+    drupal_add_js($js, array('type' => 'inline', 'scope' => 'footer'));
   }
 }
