diff --git a/modules/statistics/templates/statistics.tpl.php b/modules/statistics/templates/statistics.tpl.php
index fb4a59d18ceea7532b099b5135142e827ef8a53d..fa7e1f4e93a8c31c8d0fabbf8f76047a4d9455a9 100644
--- a/modules/statistics/templates/statistics.tpl.php
+++ b/modules/statistics/templates/statistics.tpl.php
@@ -3,61 +3,22 @@ $this->data['header'] = 'SimpleSAMLphp Statistics';
 
 $this->data['jquery'] = array('core' => true, 'ui' => true, 'css' => true);
 
-$this->data['head'] = '<link rel="stylesheet" type="text/css" href="/' .
-    $this->data['baseurlpath'] . 'module.php/statistics/style.css" />';
-$this->data['head'] .= '<script type="text/javascript">
-$(document).ready(function() {
-    $("#tabdiv").tabs();
-});
-</script>';
+$this->data['head'] = '<link rel="stylesheet" type="text/css" href="' . SimpleSAML\Module::getModuleURL("statistics/style.css") . '" />' . "\n";
+$this->data['head'] .= '<script type="text/javascript" src="' . SimpleSAML\Module::getModuleURL("statistics/javascript.js") . '"></script>' . "\n";
 
 $this->includeAtTemplateBase('includes/header.php');
 
-function getBaseURL($t, $type = 'get', $key = null, $value = null)
-{
-    $vars = array(
-        'rule' => $t->data['selected.rule'],
-        'time' => $t->data['selected.time'],
-        'res' => $t->data['selected.timeres'],
-    );
-    if (isset($t->data['selected.delimiter'])) {
-        $vars['d'] = $t->data['selected.delimiter'];
-    }
-    if (!empty($t->data['selected.rule2']) && $t->data['selected.rule2'] !== '_') {
-        $vars['rule2'] = $t->data['selected.rule2'];
-    }
-
-    if (isset($key)) {
-        if (isset($vars[$key])) {
-            unset($vars[$key]);
-        }
-        if (isset($value)) {
-            $vars[$key] = $value;
-        }
-    }
-
-    if ($type === 'get') {
-        return 'showstats.php?' . http_build_query($vars, '', '&amp;');
-    } else {
-        $text = '';
-        foreach($vars as $k => $v) {
-            $text .= '<input type="hidden" name="' . $k . '" value="'. htmlspecialchars($v) . '" />' . "\n";
-        }
-        return $text;
-    }
-}
-
 echo '<h1>'. $this->data['available.rules'][$this->data['selected.rule']]['name'] . '</h1>';
 echo '<p>' . $this->data['available.rules'][$this->data['selected.rule']]['descr'] . '</p>';
 
 // Report settings
 echo '<table class="selecttime">';
-echo '<tr><td class="selecttime_icon"><img src="../../resources/icons/crystal_project/kchart.32x32.png" alt="Report settings" /></td>';
+echo '<tr><td class="selecttime_icon"><img src="' . SimpleSAML\Utils\HTTP::getBaseUrl() . 'resources/icons/crystal_project/kchart.32x32.png" alt="Report settings" /></td>';
 
 // Select report
 echo '<td>';
 echo '<form action="#">';
-echo getBaseURL($this, 'post', 'rule');
+echo $this->data['post_rule'];
 echo '<select onchange="submit();" name="rule">';
 foreach ($this->data['available.rules'] as $key => $rule) {
     if ($key === $this->data['selected.rule']) {
@@ -72,7 +33,7 @@ echo '</td>';
 // Select delimiter
 echo '<td class="td_right">';
 echo '<form action="#">';
-echo getBaseURL($this, 'post', 'd');
+echo $this->data['post_d'];
 echo '<select onchange="submit();" name="d">';
 foreach ($this->data['availdelimiters'] as $key => $delim) {
     $delimName = $delim;
@@ -98,17 +59,17 @@ echo '</table>';
 
 // Select time and date
 echo '<table class="selecttime">';
-echo '<tr><td class="selecttime_icon"><img src="../../resources/icons/crystal_project/date.32x32.png" alt="Select date and time" /></td>';
+echo '<tr><td class="selecttime_icon"><img src="' . SimpleSAML\Utils\HTTP::getBaseUrl() . 'resources/icons/crystal_project/date.32x32.png" alt="Select date and time" /></td>';
 
 if (isset($this->data['available.times.prev'])) {
-    echo '<td><a href="' . getBaseURL($this, 'get', 'time', $this->data['available.times.prev']) . '">« Previous</a></td>';
+    echo '<td><a href="' . $this->data['get_times_prev'] . '">« Previous</a></td>';
 } else {
     echo '<td class="selecttime_link_grey">« Previous</td>';
 }
 
 echo '<td class="td_right">';
 echo '<form action="#">';
-echo getBaseURL($this, 'post', 'res');
+echo $this->data['post_res'];
 echo '<select onchange="submit();" name="res">';
 foreach ($this->data['available.timeres'] as $key => $timeresname) {
     if ($key == $this->data['selected.timeres']) {
@@ -122,7 +83,7 @@ echo '</td>';
 
 echo '<td class="td_left">';
 echo '<form action="#">';
-echo getBaseURL($this, 'post', 'time');
+echo $this->data['post_time'];
 echo '<select onchange="submit();" name="time">';
 foreach ($this->data['available.times'] as $key => $timedescr) {
     if ($key == $this->data['selected.time']) {
@@ -135,7 +96,7 @@ echo '</select></form>';
 echo '</td>';
 
 if (isset($this->data['available.times.next'])) {
-    echo '<td class="td_right td_next_right"><a href="' . getBaseURL($this, 'get', 'time', $this->data['available.times.next']) . '">Next »</a></td>';
+    echo '<td class="td_right td_next_right"><a href="' . $this->data['get_times_next'] . '">Next »</a></td>';
 } else {
     echo '<td class="td_right selecttime_link_grey td_next_right">Next »</td>';
 }
@@ -155,8 +116,8 @@ echo '
 echo '<img src="' . htmlspecialchars($this->data['imgurl']) . '" alt="Graph" />';
 
 echo '<form action="#">';
-echo '<p class="p_right">Compare with total from this dataset</p>';
-echo getBaseURL($this, 'post', 'rule2');
+echo '<p class="p_right">Compare with total from this dataset ';
+echo $this->data['post_rule2'];
 echo '<select onchange="submit();" name="rule2">';
 echo '	<option value="_">None</option>';
 foreach ($this->data['available.rules'] as $key => $rule) {
@@ -166,7 +127,7 @@ foreach ($this->data['available.rules'] as $key => $rule) {
         echo '<option value="' . $key . '">' . $rule['name'] . '</option>';
     }
 }
-echo '</select></form>';
+echo '</select></p></form>';
 
 echo '</div>'; // end graph content.
 
diff --git a/modules/statistics/templates/statmeta.tpl.php b/modules/statistics/templates/statmeta.tpl.php
index 21cd84a1f7765816083a1f6a1c507d5846124d03..9af5a032ef3ce5b9caf74256de5bbb8c9ca6b885 100644
--- a/modules/statistics/templates/statmeta.tpl.php
+++ b/modules/statistics/templates/statmeta.tpl.php
@@ -1,44 +1,42 @@
 <?php
 $this->data['header'] = 'SimpleSAMLphp Statistics Metadata';
+$this->data['head'] = '<link rel="stylesheet" type="text/css" href="' . SimpleSAML\Module::getModuleURL("statistics/style.css") . '" />';
 $this->includeAtTemplateBase('includes/header.php');
 
 echo '<table id="statmeta">' ;
 
 if (isset($this->data['metadata'])) {
-    if (isset($this->data['metadata']['lastrun'] )) {
-        echo '<tr><td>Aggregator last run at</td><td>' . 
-            date('l jS \of F Y H:i:s', $this->data['metadata']['lastrun']) . '</td></tr>';
+    $metadata = $this->data['metadata'];
+
+    if (isset($metadata['lastrun'])) {
+        echo '<tr><td>Aggregator last run at</td><td>' . $metadata['lastrun'] . '</td></tr>';
     }
 
-    if (isset($this->data['metadata']['notBefore'] )) {
-        echo '<tr><td>Aggregated data until</td><td>' . 
-            date('l jS \of F Y H:i:s', $this->data['metadata']['notBefore']) . '</td></tr>';
+    if (isset($metadata['notBefore'])) {
+        echo '<tr><td>Aggregated data until</td><td>' . $metadata['notBefore'] . '</td></tr>';
     }
 
-    if (isset($this->data['metadata']['memory'] )) {
-        echo '<tr><td>Memory usage</td><td>' . 
-            number_format($this->data['metadata']['memory'] / (1024*1024), 2) . ' MB' . '</td></tr>';
+    if (isset($metadata['memory'])) {
+        echo '<tr><td>Memory usage</td><td>' . $metadata['memory'] . ' MB' . '</td></tr>';
     }
 
-    if (isset($this->data['metadata']['time'] )) {
-        echo '<tr><td>Execution time</td><td>' . 
-            $this->data['metadata']['time'] . ' seconds' . '</td></tr>';
+    if (isset($metadata['time'])) {
+        echo '<tr><td>Execution time</td><td>' . $metadata['time'] . ' seconds' . '</td></tr>';
     }
 
-    if (isset($this->data['metadata']['lastlinehash'] )) {
-        echo '<tr><td>SHA1 of last processed logline</td><td>' . 
-            $this->data['metadata']['lastlinehash'] . '</td></tr>';
+    if (isset($metadata['lastlinehash'] )) {
+        echo '<tr><td>SHA1 of last processed logline</td><td>' . $metadata['lastlinehash'] . '</td></tr>';
     }
 
-    if (isset($this->data['metadata']['lastline'] )) {
-        echo '<tr><td>Last processed logline</td><td>' . 
-            $this->data['metadata']['lastline'] . '</td></tr>';
+    if (isset($metadata['lastline'] )) {
+        echo '<tr><td>Last processed logline</td><td>' . $metadata['lastline'] . '</td></tr>';
     }
 } else {
     echo '<tr><td>No metadata found</td></tr>';
 }
 
 echo '</table>';
-echo '<p>[ <a href="showstats.php">Show statistics</a> ] </p>';
+echo '<p>[ <a href="' . SimpleSAML\Module::getModuleURL("statistics/showstats.php") . '">Show statistics</a> ] </p>';
 
 $this->includeAtTemplateBase('includes/footer.php');
+
diff --git a/modules/statistics/www/javascript.js b/modules/statistics/www/javascript.js
new file mode 100644
index 0000000000000000000000000000000000000000..a5aa421a67fffda6c3c2f4527c8227e58b1b9cde
--- /dev/null
+++ b/modules/statistics/www/javascript.js
@@ -0,0 +1,4 @@
+$(document).ready(function() {
+        $("#tabdiv").tabs();
+});
+
diff --git a/modules/statistics/www/showstats.php b/modules/statistics/www/showstats.php
index b8063c79dc0f539fa8cdc1d836ad74300df9c4df..051dc9aa55440fd3cd87059ce0a15fe6ab1dc302 100644
--- a/modules/statistics/www/showstats.php
+++ b/modules/statistics/www/showstats.php
@@ -119,4 +119,47 @@ $t->data['topdelimiters'] = $dataset->getTopDelimiters();
 $t->data['availdelimiters'] = $dataset->availDelimiters();
 
 $t->data['delimiterPresentation'] =  $dataset->getDelimiterPresentation();
+
+$t->data['post_rule'] = getBaseURL($t, 'post', 'rule');
+$t->data['post_rule2'] = getBaseURL($t, 'post', 'rule2');
+$t->data['post_d'] = getBaseURL($t, 'post', 'd');
+$t->data['post_res'] = getBaseURL($t, 'post', 'res');
+$t->data['post_time'] = getBaseURL($t, 'post', 'time');
+$t->data['get_times_prev'] = getBaseURL($t, 'get', 'time', $t->data['available.times.prev']);
+$t->data['get_times_next'] = getBaseURL($t, 'get', 'time', $t->data['available.times.next']);
+
 $t->show();
+
+function getBaseURL($t, $type = 'get', $key = null, $value = null)
+{
+    $vars = array(
+        'rule' => $t->data['selected.rule'],
+        'time' => $t->data['selected.time'],
+        'res' => $t->data['selected.timeres'],
+    );
+    if (isset($t->data['selected.delimiter'])) {
+        $vars['d'] = $t->data['selected.delimiter'];
+    }
+    if (!empty($t->data['selected.rule2']) && $t->data['selected.rule2'] !== '_') {
+        $vars['rule2'] = $t->data['selected.rule2'];
+    }
+
+    if (isset($key)) {
+        if (isset($vars[$key])) {
+            unset($vars[$key]);
+        }
+        if (isset($value)) {
+            $vars[$key] = $value;
+        }
+    }
+
+    if ($type === 'get') {
+        return SimpleSAML\Module::getModuleURL("statistics/showstats.php") . '?' . http_build_query($vars, '', '&amp;');
+    } else {
+        $text = '';
+        foreach($vars as $k => $v) {
+            $text .= '<input type="hidden" name="' . $k . '" value="'. htmlspecialchars($v) . '" />' . "\n";
+        }
+        return $text;
+    }
+}
diff --git a/modules/statistics/www/statmeta.php b/modules/statistics/www/statmeta.php
index 4b8a9d04b78695bf1c180801762e110f0c6fcb5a..0959bf1e501e986e5f53dfb687bca6a90b4ce468 100644
--- a/modules/statistics/www/statmeta.php
+++ b/modules/statistics/www/statmeta.php
@@ -9,8 +9,20 @@ $aggr = new sspmod_statistics_Aggregator();
 $aggr->loadMetadata();
 $metadata = $aggr->getMetadata();
 
-
 $t = new SimpleSAML_XHTML_Template($config, 'statistics:statmeta.tpl.php');
-$t->data['metadata'] =  $metadata;
+
+if ($metadata !== null) {
+    if (in_array('lastrun', $metadata)) {
+        $metadata['lastrun'] = date('l jS \of F Y H:i:s', $metadata['lastrun']);
+    }
+    if (in_array('notBefore', $metadata)) {
+        $metadata['notBefore'] = date('l jS \of F Y H:i:s', $metadata['notBefore']);
+    }
+    if (in_array('memory', $metadata)) {
+        $metadata['memory'] = number_format($metadata['memory'] / (1024 * 1024), 2);
+    }
+    $t->data['metadata'] = $metadata;
+}
+
 $t->show();