$(document).ready(function() {
  var $pre = $('pre');
  var log = function(m) {
    $pre.append(m).append("\r\n");
  }
  var get = // adapted from lodash.get, with thanks
    function(o,p,v) {
      var i=0, l=p.length;
      while(o && i<l) { o = o[p[i++]]; }
      return o ? o : v;
    }
  log("Loading database");
$.getJSON("/api/db-dump", function(db_dump){
  log("Database loaded");
  if (!db_dump.pico) {
    log("Creating owner Pico");
    $.getJSON("/api/new-pico", function(d) {
      if (d && d.id) {
        log("Pico created: "+d.id);
        log("Creating owner channel");
        $.getJSON("/api/pico/"+d.id+"/new-channel",
            {"name": "main", "type": "secret"},
            function(c){
              if (c && c.id) {
                log("Owner Pico ECI: "+c.id);
              } else {
                log("*Problem creating owner Pico channel");
              }
            }
        );
      } else {
        log("*Problem creating owner Pico");
      }
    });
  } else {
    log("Database has an owner Pico");
  }
  if (!db_dump.rulesets) {
    log("Registering rulesets");
    log("Getting io.picolabs.pico.krl");
    $.get("https://raw.githubusercontent.com/Picolab/node-pico-engine/master/krl/io.picolabs.pico.krl",function(k){
      if (k && k.length > 0) {
        log("io.picolabs.pico.krl length: "+k.length);
        log("Registering io.picolabs.pico");
        $.getJSON("/api/ruleset/register",{"src":k},function(r){
          if (r && r.ok) {
            log("io.picolabs.pico registered");
          } else {
            log("*Problem registering io.picolabs.pico");
          }
        });
      } else {
        log("*Problem getting io.picolabs.pico.krl");
      }
    },"text");
    log("Getting io.picolabs.visual_params.krl");
    $.get("https://raw.githubusercontent.com/Picolab/node-pico-engine/master/krl/io.picolabs.visual_params.krl",function(k){
      if (k && k.length > 0) {
        log("io.picolabs.visual_params.krl length: "+k.length);
        log("Registering io.picolabs.visual_params");
        $.getJSON("/api/ruleset/register",{"src":k},function(r){
          if (r && r.ok) {
            log("io.picolabs.visual_params registered");
          } else {
            log("*Problem registering io.picolabs.visual_params");
          }
        });
      } else {
        log("*Problem getting io.picolabs.visual_params.krl");
      }
    },"text");
  } else {
    log("Database has rulesets");
  }
});
});

@@ -315,133 +315,51 @@
 %22);%0A
-$.getJSON(%22/api/db-dump%22, function(db_dump)%7B%0A  log(%22Database loaded%22);%0A  if (!db_dump.pico) %7B%0A    log(%22Creating owner Pico%22);
+  var createOwnerPico = function(callback)%7B
 %0A   
@@ -908,157 +908,79 @@
 %0A  %7D
- else %7B%0A    log(%22Database has an owner Pico%22);%0A  %7D%0A  if (!db_dump.rulesets) %7B%0A    log(%22Registering rulesets%22);%0A    log(%22Getting io.picolabs.pico.krl%22
+;%0A  var installRuleset = function(rid,callback)%7B%0A    log(%22Getting %22+rid
 );%0A 
@@ -1059,32 +1059,23 @@
 ter/krl/
-io.picolabs.pico
+%22+rid+%22
 .krl%22,fu
@@ -1128,33 +1128,21 @@
     log(
-%22io.picolabs.pico
+rid+%22
 .krl len
@@ -1184,33 +1184,21 @@
 stering 
-io.picolabs.pico%22
+%22+rid
 );%0A     
@@ -1304,25 +1304,13 @@
 log(
-%22io.picolabs.pico
+rid+%22
  reg
@@ -1381,25 +1381,13 @@
 ing 
-io.picolabs.pico%22
+%22+rid
 );%0A 
@@ -1458,29 +1458,13 @@
 ing 
-io.picolabs.pico.krl%22
+%22+rid
 );%0A 
@@ -1491,168 +1491,37 @@
 ;%0A  
-  log(%22Getting io.picolabs.visual_params.krl%22);%0A    $.get(%22https://raw.githubusercontent.com/Picolab/node-pico-engine/master/krl/io.picolabs.visual_params.krl
+%7D;%0A$.getJSON(%22/api/db-dump
 %22,
+ 
 func
@@ -1529,517 +1529,321 @@
 ion(
-k)%7B%0A      if (k && k.length %3E 0) %7B%0A        log(%22io.picolabs.visual_params.krl length: %22+k.length);%0A        log(%22Registering io.picolabs.visual_params%22);%0A        $.getJSON(%22/api/ruleset/register%22,%7B%22src%22:k%7D,function(r)%7B%0A          if (r && r.ok) %7B%0A            log(%22io.picolabs.visual_params registered%22);%0A          %7D else %7B%0A            log(%22*Problem registering io.picolabs.visual_params%22);%0A          %7D%0A        %7D);%0A      %7D else %7B%0A        log(%22*Problem getting io.picolabs.visual_params.krl%22);%0A      %7D%0A    %7D,%22text
+db_dump)%7B%0A  log(%22Database loaded%22);%0A  if (!db_dump.pico) %7B%0A    log(%22Creating owner Pico%22);%0A    createOwnerPico();%0A  %7D else %7B%0A    log(%22Database has an owner Pico%22);%0A  %7D%0A  if (!db_dump.rulesets) %7B%0A    log(%22Registering rulesets%22);%0A    installRuleset(%22io.picolabs.pico%22);%0A    installRuleset(%22io.picolabs.visual_params
 %22);%0A

rearrange the code