'use strict';
var wd;

// load the driver
var Driver = require('./index.js');
// initialize the driver
var driver = new Driver({
  host: '127.0.1.1'
});
// start the driver
driver.start(
  // callback invoked when driver is started
  function(options) {
    // load, connect and initialize WD.js
    wd = require('wd').promiseChainRemote(options.wd);
    wd.init(options.wd).then(
      haveSomeFunWithWebDriver,
      console.error.bind(console)
    );
  },
  // callback invoked when driver could not start
  console.error.bind(console),
  // callback invoked when driver crashed after successful start
  console.error.bind(console)
);

function stop() {
  // stop the client
  wd.quit().then(function() {
    // then stop the driver
    driver.stop(function() {
      // now we're done
      console.log("bye bye");
    });
  });
}

function haveSomeFunWithWebDriver() {
  // open a website
  wd.get('https://google.com')
    // find all its links
    .elements('css', 'a')
    // output what ever elements() returned
    .then(function(data) {
      console.log("success", JSON.stringify(data, null, 2));
    }, function(data) {
      console.log("error", JSON.stringify(data, null, 2));
    })
    // stop WD and the driver
    .then(stop)
    // woopsi
    .catch(function(error) {
      console.error("failure", error);
      stop();
    })
    // promises…
    .done();
}
@@ -149,493 +149,8 @@
 %0A%7D);
-%0A// start the driver%0Adriver.start(%0A  // callback invoked when driver is started%0A  function(options) %7B%0A    // load, connect and initialize WD.js%0A    wd = require('wd').promiseChainRemote(options.wd);%0A    wd.init(options.wd).then(%0A      haveSomeFunWithWebDriver,%0A      console.error.bind(console)%0A    );%0A  %7D,%0A  // callback invoked when driver could not start%0A  console.error.bind(console),%0A  // callback invoked when driver crashed after successful start%0A  console.error.bind(console)%0A);
 %0A%0Afu
@@ -319,17 +319,17 @@
 log(
-%22
+'
 bye bye
-%22
+'
 );%0A 
@@ -578,17 +578,17 @@
 log(
-%22
+'
 success
-%22
+'
 , JS
@@ -663,15 +663,15 @@
 log(
-%22
+'
 error
-%22
+'
 , JS
@@ -820,17 +820,17 @@
 ror(
-%22
+'
 failure
-%22
+'
 , er
@@ -883,12 +883,499 @@
   .done();%0A%7D
+%0A%0A// start the driver%0Adriver.start(%0A  // callback invoked when driver is started%0A  function(options) %7B%0A    // load, connect and initialize WD.js%0A    wd = require('wd').promiseChainRemote(options.wd);%0A    wd.init(options.wd).then(%0A      haveSomeFunWithWebDriver,%0A      console.error.bind(console)%0A    );%0A  %7D,%0A  // callback invoked when driver could not start%0A  console.error.bind(console),%0A  // callback invoked when driver crashed after successful start%0A  console.error.bind(console)%0A);%0A

Change function arrangement