var utils = require("radiodan-client").utils,
    logger = utils.logger(__filename);

module.exports = function(radiodan){
  return function() {
    clearPlayers(radiodan.cache.players).then(
      function() {
        process.exit(0);
      },
      function() {
        process.exit(1);
      }
    );

    function clearPlayers(players) {
      var playerIds = Object.keys(players),
          resolved = utils.promise.resolve();

      if(playerIds.length === 0){
        return resolved;
      }

      return Object.keys(players).reduce(function(previous, current){
        return previous.then(players[current].clear);
      }, resolved);
    }
  };
};

@@ -141,173 +141,274 @@
 () %7B
-%0A    clearPlayers(radiodan.cache.players).then(%0A      function() %7B%0A        process.exit(0);%0A      %7D,%0A      function() %7B%0A        process.exit
+ return gracefulExit(radiodan); %7D%0A%7D;%0A%0Afunction gracefulExit(radiodan) %7B%0A  // if promise does not resolve quickly enough, exit anyway%0A  setTimeout(exitWithCode(2), 3000);%0A%0A  return clearPlayers(radiodan.cache.players)%0A    .then(exitWithCode(0), exitWithCode
 (1)
+)
 ;%0A
-      %7D%0A    );%0A%0A    
+%7D;%0A%0A
 func
@@ -442,101 +442,96 @@
 %7B%0A  
-    var playerIds = Object.keys(players),%0A          resolved = utils.promise.resolve();%0A%0A    
+var resolved  = utils.promise.resolve(),%0A      playerIds = Object.keys(players %7C%7C %7B%7D);%0A%0A
   if
@@ -556,20 +556,16 @@
 === 0)%7B%0A
-    
     retu
@@ -583,19 +583,11 @@
 ;%0A  
-    %7D%0A%0A    
+%7D%0A%0A
   re
@@ -652,20 +652,16 @@
 t)%7B%0A    
-   
- 
 return p
@@ -698,20 +698,16 @@
 clear);%0A
-    
   %7D, res
@@ -718,18 +718,134 @@
 d);%0A
-    %7D
+%7D%0A%0Afunction exitWithCode(exitCode) %7B%0A  return function() %7B%0A    process.exit(exitCode);%0A%0A    return utils.promise.resolve();
 %0A  %7D
-;
 %0A%7D
-;
 %0A

Exit process if clearing process does not finish within 3 seconds