Index: libfdcore/config.c
===================================================================
--- libfdcore/config.c	(revision 662)
+++ libfdcore/config.c	(revision 686)
@@ -457,4 +457,7 @@
 	TRACE_ENTRY();
 	
+	if (!fd_g_config)
+		return 0;
+	
 	/* Free the TLS parameters */
 	gnutls_priority_deinit(fd_g_config->cnf_sec_data.prio_cache);
Index: libfdcore/core.c
===================================================================
--- libfdcore/core.c	(revision 662)
+++ libfdcore/core.c	(revision 686)
@@ -39,6 +39,6 @@
 
 /* The static configuration structure */
-static struct fd_config conf;
-struct fd_config * fd_g_config = &conf;
+static struct fd_config g_conf;
+struct fd_config * fd_g_config = NULL;
 
 /* gcrypt functions to support posix threads */
@@ -62,8 +62,15 @@
 /* Thread that process incoming events on the main queue -- and terminates the framework when requested */
 static pthread_t core_runner = (pthread_t)NULL;
+enum core_mode {
+	CORE_MODE_EVENTS,
+	CORE_MODE_IMMEDIATE
+};
 
 static void * core_runner_thread(void * arg) 
 {
 	fd_log_threadname("Core Runner");
+	
+	if (arg && (*(int *)arg == CORE_MODE_IMMEDIATE))
+		goto end;
 	
 	/* Handle events incoming on the main event queue */
@@ -161,6 +168,4 @@
 	int ret;
 	
-	memset(fd_g_config, 0, sizeof(struct fd_config));
-	
 	/* Initialize the library -- must come first since it initializes the debug facility */
 	ret = fd_libproto_init();
@@ -187,4 +192,6 @@
 	
 	/* Initialize the config with default values */
+	memset(&g_conf, 0, sizeof(struct fd_config));
+	fd_g_config = &g_conf;
 	CHECK_FCT( fd_conf_init() );
 
@@ -269,6 +276,12 @@
 int fd_core_shutdown(void)
 {
-	/* Signal the framework to terminate */
-	CHECK_FCT( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL) );
+	if (core_runner != (pthread_t)NULL) {
+		/* Signal the framework to terminate */
+		CHECK_FCT( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL) );
+	} else {
+		/* The framework was maybe not fully initialized (ex: tests) */
+		enum core_mode arg = CORE_MODE_IMMEDIATE;
+		(void) core_runner_thread(&arg);
+	}
 	
 	return 0;
@@ -282,15 +295,17 @@
 	void * th_ret = NULL;
 	
-	/* Just wait for core_runner_thread to complete and return gracefully */
-	ret = pthread_join(core_runner, &th_ret);
-	if (ret != 0) {
-		fprintf(stderr, "Unable to wait for main framework thread termination: %s\n", strerror(ret));
-		return ret;
-	}
-	
-	return 0;
-}
-
-
-
-
+	if (core_runner != (pthread_t)NULL) {
+		/* Just wait for core_runner_thread to complete and return gracefully */
+		ret = pthread_join(core_runner, &th_ret);
+		if (ret != 0) {
+			fprintf(stderr, "Unable to wait for main framework thread termination: %s\n", strerror(ret));
+			return ret;
+		}
+	}
+	
+	return 0;
+}
+
+
+
+
Index: libfdcore/p_expiry.c
===================================================================
--- libfdcore/p_expiry.c	(revision 662)
+++ libfdcore/p_expiry.c	(revision 686)
@@ -39,6 +39,6 @@
 #define GC_TIME		120
 
-static pthread_t       exp_thr;
-static pthread_t       gc_thr;
+static pthread_t       exp_thr = (pthread_t)NULL;
+static pthread_t       gc_thr  = (pthread_t)NULL;
 static struct fd_list  exp_list = FD_LIST_INITIALIZER( exp_list );
 static pthread_cond_t  exp_cnd  = PTHREAD_COND_INITIALIZER;
Index: libfdcore/queues.c
===================================================================
--- libfdcore/queues.c	(revision 662)
+++ libfdcore/queues.c	(revision 686)
@@ -60,4 +60,8 @@
 	
 	/* Note : the threads that post into this queue should already been stopped before this !!! */
+	
+	CHECK_PARAMS(queue);
+	if (*queue == NULL)
+		return 0; /* the queue was not already initialized */
 
 	/* Empty all contents */
Index: libfdcore/routing_dispatch.c
===================================================================
--- libfdcore/routing_dispatch.c	(revision 662)
+++ libfdcore/routing_dispatch.c	(revision 686)
@@ -973,9 +973,9 @@
 
 /* Threads report their status */
-enum thread_state { INITIAL = 0, RUNNING = 1, TERMINATED = 2 };
+enum thread_state { NOTRUNNING = 0, RUNNING = 1 };
 static void cleanup_state(void * state_loc)
 {
 	if (state_loc)
-		*(enum thread_state *)state_loc = TERMINATED;
+		*(enum thread_state *)state_loc = NOTRUNNING;
 }
 
@@ -1080,8 +1080,8 @@
 /* Later: make this more dynamic */
 static pthread_t rt_out = (pthread_t)NULL;
-static enum thread_state out_state = INITIAL;
+static enum thread_state out_state = NOTRUNNING;
 
 static pthread_t rt_in  = (pthread_t)NULL;
-static enum thread_state in_state = INITIAL;
+static enum thread_state in_state = NOTRUNNING;
 
 /* Initialize the routing and dispatch threads */
@@ -1091,6 +1091,6 @@
 	
 	/* Prepare the array for dispatch */
+	CHECK_MALLOC( disp_state = calloc(fd_g_config->cnf_dispthr, sizeof(enum thread_state)) );
 	CHECK_MALLOC( dispatch = calloc(fd_g_config->cnf_dispthr, sizeof(pthread_t)) );
-	CHECK_MALLOC( disp_state = calloc(fd_g_config->cnf_dispthr, sizeof(enum thread_state)) );
 	
 	/* Create the threads */
@@ -1126,5 +1126,5 @@
 	/* Wait for a second for the thread to complete, by monitoring my_state */
 	fd_cpu_flush_cache();
-	if (*st != TERMINATED) {
+	if (*st != NOTRUNNING) {
 		TRACE_DEBUG(INFO, "Waiting for the %s thread to have a chance to terminate", th_name);
 		do {
@@ -1137,5 +1137,6 @@
 			
 			while (TS_IS_INFERIOR( &ts, &ts_final )) {
-				if (*st == TERMINATED)
+				fd_cpu_flush_cache();
+				if (*st == NOTRUNNING)
 					break;
 				
@@ -1171,7 +1172,15 @@
 	CHECK_FCT_DO( fd_queues_fini(&fd_g_local), /* ignore */);
 	
-	/* Stop the Dispatch thread */
-	for (i=0; i < fd_g_config->cnf_dispthr; i++) {
-		stop_thread_delayed(&disp_state[i], &dispatch[i], "Dispatching");
+	/* Stop the Dispatch threads */
+	if (dispatch != NULL) {
+		for (i=0; i < fd_g_config->cnf_dispthr; i++) {
+			stop_thread_delayed(&disp_state[i], &dispatch[i], "Dispatching");
+		}
+		free(dispatch);
+		dispatch = NULL;
+	}
+	if (disp_state != NULL) {
+		free(disp_state);
+		disp_state = NULL;
 	}
 	
Index: libfdcore/server.c
===================================================================
--- libfdcore/server.c	(revision 662)
+++ libfdcore/server.c	(revision 686)
@@ -36,8 +36,8 @@
 #include "fdcore-internal.h"
 
-/* Server (listening) part of the daemon */
-
-struct fd_list		FD_SERVERS = FD_LIST_INITIALIZER(FD_SERVERS);	/* The list of all server objects */
-/* We don't need to protect this list, it is only accessed from the main daemon thread. */
+/* Server (listening) part of the framework */
+
+static struct fd_list	FD_SERVERS = FD_LIST_INITIALIZER(FD_SERVERS);	/* The list of all server objects */
+/* We don't need to protect this list, it is only accessed from the main framework thread. */
 
 /* Servers information */
@@ -83,5 +83,5 @@
 							  "Thread status unknown")));
 		/* Dump the client list of this server */
-		(void) pthread_mutex_lock(&s->clients_mtx);
+		CHECK_POSIX_DO( pthread_mutex_lock(&s->clients_mtx), );
 		for (cli = s->clients.next; cli != &s->clients; cli = cli->next) {
 			struct client * c = (struct client *)cli;
@@ -91,5 +91,5 @@
 					fd_log_time(&c->ts, bufts, sizeof(bufts)));
 		}
-		(void) pthread_mutex_unlock(&s->clients_mtx);
+		CHECK_POSIX_DO( pthread_mutex_unlock(&s->clients_mtx), );
 	}
 }
Index: libfdproto/messages.c
===================================================================
--- libfdproto/messages.c	(revision 658)
+++ libfdproto/messages.c	(revision 686)
@@ -1109,6 +1109,6 @@
 
 /******************* End-to-end counter *********************/
-uint32_t fd_eteid;
-pthread_mutex_t fd_eteid_lck = PTHREAD_MUTEX_INITIALIZER;
+static uint32_t fd_eteid;
+static pthread_mutex_t fd_eteid_lck = PTHREAD_MUTEX_INITIALIZER;
 
 void fd_msg_eteid_init(void)
Index: libfdproto/sessions.c
===================================================================
--- libfdproto/sessions.c	(revision 662)
+++ libfdproto/sessions.c	(revision 686)
@@ -122,5 +122,5 @@
 static pthread_mutex_t	exp_lock = PTHREAD_MUTEX_INITIALIZER;	/* lock protecting the list. */
 static pthread_cond_t	exp_cond = PTHREAD_COND_INITIALIZER;	/* condvar used by the expiry mecahinsm. */
-static pthread_t	exp_thr; 	/* The expiry thread that handles cleanup of expired sessions */
+static pthread_t	exp_thr = (pthread_t)NULL; 	/* The expiry thread that handles cleanup of expired sessions */
 
 /* Hierarchy of the locks, to avoid deadlocks:
