Fixed shutting down. Crazy threads. And I even managed to get it working without a global variable.