diff --git a/exec/ircd.js b/exec/ircd.js
index fc36150f9c3cec6a190150ea45e5a161e2c52c54..63130aa1caca177507aa20220d9ad9f6c0e46775 100644
--- a/exec/ircd.js
+++ b/exec/ircd.js
@@ -913,6 +913,8 @@ while (!server.terminated) {
 
 	if(server.terminated)
 		break;
+	if(this.js!=undefined && js.terminated)
+		break;
 
 	// Setup a new socket if a connection is accepted.
 	for (pl in open_plines) {
@@ -3229,7 +3231,9 @@ function IRCClient_registered_commands(command, cmdline) {
 				break;
 			}
 			log("!ERROR! Shutting down the ircd as per " + this.ircnuh);
-			js.terminated = true;
+			if(this.js!=undefined)
+				js.terminated = true;
+			server.terminated = true;
 			break;
 		case "REHASH":
 			if (!((this.mode&USERMODE_OPER) &&