diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index ba37a9faa1756f97bc068e67596dafc546627964..080c507b0b88f84e734a54abe5a8c3fa6cd76863 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -34,6 +34,7 @@ bool database_init  (const char *dbpath);
 bool database_open  (volatile sqlite3 *db, const char *dbpath, bool check);
 bool database_attach(volatile sqlite3 *db, const char *name, const char *dbpath);
 bool database_detach(volatile sqlite3 *db, const char *name);
+void database_close_all(void);
 
 /* Get information on the queue and currently playing kara. */
 bool database_queue_state       (volatile sqlite3 *db, struct lkt_queue_state *res);
diff --git a/src/commands.c b/src/commands.c
index cb463212bd380491b7c25df2d8c088e9d74baf32..9ad34084ef1317c2dadf1b3ef0b0e2488e2a49b6 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -29,6 +29,7 @@ command_restart(struct lkt_state *srv, size_t c)
         return false;
     }
     close(srv->fds[0].fd);
+    database_close_all();
     execv(executable_name, (char *const *) argv);
     LOG_ERROR_SCT("GENERAL", "%s", "Failed to exec lektor or OS not supported");
     return false;
@@ -73,6 +74,7 @@ command_kill(struct lkt_state *srv, size_t c)
     RETURN_UNLESS(lkt_client_auth(srv, c, false), "Failed to authentificate user", false);
     LOG_INFO_SCT("GENERAL", "%s", "Stopping lektord");
     close(srv->fds[0].fd);
+    database_close_all();
     exit(EXIT_SUCCESS);
 }
 
diff --git a/src/database/open.c b/src/database/open.c
index 497d71ff95c62bd2f6c88ff1bb7035e2bb9d89f2..6cc01a60cfb485142881b78f45942a949334db73 100644
--- a/src/database/open.c
+++ b/src/database/open.c
@@ -31,14 +31,16 @@ __dec(volatile sqlite3 *db, const char *name)
     safe_snprintf(SQL, LKT_MAX_SQLITE_STATEMENT,
                   "UPDATE %s.misc SET opened = 0;", name);
     SQLITE_EXEC(db, SQL, error);
+    LOG_INFO_SCT("DB", "Dec open count on db '%s'", name);
     return;
 error:
     LOG_ERROR_SCT("DB", "%s", "Faield to close database");
 }
 
 void
-__close_databases(void)
+database_close_all(void)
 {
+    LOG_INFO_SCT("GARBAGE", "%s", "Closing all db");
     if (pthread_mutex_trylock(&mtx))
         return;
     struct named_db *item;
@@ -57,23 +59,20 @@ __inc(volatile sqlite3 *db, const char *name)
 {
     SQLITE_EXEC(db, "UPDATE misc SET opened = (SELECT opened + 1 FROM misc);",
                 error);
+
     if (!atexited) {
         atexited = 1;
         if (stack_new(&db_stack))
             goto out_of_memory;
-        if (atexit(__close_databases)) {
-            LOG_ERROR_SCT("GENERAL", "%s",
-                          "Failed to set function in 'atexit'");
-            exit(EXIT_FAILURE);
-        }
-        struct named_db *item = malloc(sizeof(struct named_db));
-        if (!item)
-            goto out_of_memory;
-        item->name = strdup(name);
-        item->db   = db;
-        if (stack_push(&db_stack, item))
-            goto out_of_memory;
     }
+
+    struct named_db *item = malloc(sizeof(struct named_db));
+    if (!item)
+        goto out_of_memory;
+    item->name = strdup(name);
+    item->db   = db;
+    if (stack_push(&db_stack, item))
+        goto out_of_memory;
     return;
 error:
     LOG_ERROR_SCT("DB", "%s", "Database already in use");
diff --git a/src/main/server.c b/src/main/server.c
index ba5f5febcfb84429c6a16d5179d129b8f8fe8c23..eeae1f99388b6dba860c986b5f532ab432347f2b 100644
--- a/src/main/server.c
+++ b/src/main/server.c
@@ -35,30 +35,6 @@ REG_ADD(load_x11)
 #endif
 REG_END()
 
-char *db_path   = NULL;
-char *kara_dir  = NULL;
-char *conf_file = NULL;
-struct lkt_state srv;
-
-static void
-__garbage_collect(void)
-{
-    if (db_path)
-        free(db_path);
-    if (kara_dir)
-        free(kara_dir);
-    if (conf_file)
-        free(conf_file);
-    LOG_INFO_SCT("GARBAGE", "%s", "Cleaning a bit with global variables");
-}
-
-static void
-__sig_exit(int sig)
-{
-    LOG_ERROR_SCT("SIGNAL", "Exit because of signal %d", sig);
-    exit(EXIT_FAILURE);
-}
-
 int
 main(int argc, char *argv[])
 {
@@ -67,9 +43,6 @@ main(int argc, char *argv[])
     pthread_t th;
     executable_name = "lektord";
     UNUSED(argv);
-    assert(!signal(SIGTERM, __sig_exit));
-    assert(!signal(SIGQUIT, __sig_exit));
-    assert(!signal(SIGINT, __sig_exit));
 
     if (argc <= 1)
         goto normal_launch;
@@ -88,21 +61,23 @@ normal_launch:
     executable_name = exe;
 
     /* Init the server */
+    struct lkt_state srv;
     int autoclear, check_exclusive = 1;
-    assert(!atexit(__garbage_collect));
-    db_path   = safe_malloc(PATH_MAX * sizeof(char));
-    kara_dir  = safe_malloc(PATH_MAX * sizeof(char));
-    conf_file = safe_malloc(PATH_MAX * sizeof(char));
+    char *db_path   = safe_malloc(PATH_MAX * sizeof(char));
+    char *conf_file = safe_malloc(PATH_MAX * sizeof(char));
+    char *kara_dir  = safe_malloc(PATH_MAX * sizeof(char));
     memset(&srv, 0, sizeof(struct lkt_state));
 
     /* Initialize the system. */
     RETURN_UNLESS(database_new(&srv.db),                   "Failed to initialize the memory database", 1);
     RETURN_IF    (config_detect_file(conf_file, PATH_MAX), "Failed to find a config file",             1);
     RETURN_IF    (config_new(srv.db, conf_file),           "Failed to read configuration file",        1);
+    free(conf_file);
 
     /* Finish to initialize. */
     database_config_get_text(srv.db, "database", "db_path", db_path, PATH_MAX);
     RETURN_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database", 1);
+    free(db_path);
 
     /* Read the configuration. We already know that the config is valid */
     database_config_get_int (srv.db, "player",   "autoclear", &autoclear);
@@ -112,10 +87,9 @@ normal_launch:
 
     if (kara_dir[strlen(kara_dir) - 1] != '/')
         strncat(kara_dir, "/", PATH_MAX - 1);
-
     srv.kara_prefix = kara_dir;
-    database_config_queue_default(srv.db);
 
+    database_config_queue_default(srv.db);
     if (autoclear)
         database_queue_clear(srv.db);
 
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 1052462a8bf5f6dcdef3c3237fe863297281b451..e2f75ec4dad88d46a1334a7c2ae185d37f1829ff 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -8,6 +8,7 @@
 #include <lektor/thread.h>
 
 #include <sched.h>
+#include <assert.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -98,7 +99,6 @@ init_mpv_gl__(mpv_handle *mpv, mpv_render_context **mpv_gl,
     mpv_render_context_set_update_callback(*mpv_gl, on_mpv_render_update, NULL);
     return 0;
 }
-/* Thread related functions */
 
 static void *
 sdl_thread__(struct poller_thread_arg *arg)
@@ -157,6 +157,8 @@ sdl_thread__(struct poller_thread_arg *arg)
 
 loop:
     SDL_WaitEvent(&event);
+    if (sdl2->launched == 2)
+        goto end;
 
     switch (event.type) {
     case SDL_QUIT:
@@ -263,6 +265,9 @@ loop:
 
     sched_yield();
     goto loop;  /* A loop without indentation. */
+end:
+    sdl2->launched = 0;
+    return NULL;
 }
 
 /* Exported functions */
@@ -344,9 +349,9 @@ module_sdl2_free(struct lkt_win *const win)
 {
     RETURN_UNLESS(win && win->window, "Invalid arguments", NOTHING);
     struct module_sdl2_window *sdl2 = win->window;
-    mpv_render_context_free((mpv_render_context *) sdl2->mpv_gl);
-    sdl2->mpv_gl = NULL;
-    lmpv_free((mpv_handle **) &sdl2->mpv);
+    sdl2->launched = 2;
+    while (sdl2->launched)
+        sched_yield();
 }
 
 bool
@@ -393,16 +398,6 @@ module_sdl2_get_elapsed(struct lkt_win *const win, int *elapsed_sec)
     return true;
 }
 
-void
-module_sdl2_attach(struct lkt_win *const win, void *server)
-{
-    RETURN_UNLESS(win && server, "Invalid arguments", NOTHING);
-    struct lkt_state *srv = server;
-    struct module_sdl2_window *sdl2 = win->window;
-    sdl2->db = srv->db;
-    sdl2->mpd_idle_events = &srv->mpd_idle_events;
-}
-
 bool
 module_sdl2_handle_events(struct lkt_win *const win, volatile sqlite3 *db,
                           mpd_idle_flag *mpd_idle_events)
diff --git a/src/module/module_x11.c b/src/module/module_x11.c
index 6b1d4572f84083ae3a9dabbb3580cc347f7545f5..d9231806ea682d52f5f3d3984b9c9b2b5990618a 100644
--- a/src/module/module_x11.c
+++ b/src/module/module_x11.c
@@ -222,12 +222,6 @@ module_x11_free(struct lkt_win *const win)
     win->window = NULL;
 }
 
-void
-module_x11_attach(struct lkt_win *const win, void *server)
-{
-    UNUSED(win, server);
-}
-
 bool
 module_x11_toggle_pause(struct lkt_win *const win)
 {