debug/error messages, source code cleanup
diff --git a/src/mod_netconf.c b/src/mod_netconf.c
index da007b5..f2e8a03 100644
--- a/src/mod_netconf.c
+++ b/src/mod_netconf.c
@@ -208,14 +208,14 @@
 {
 	json_object **err_reply_p = (json_object **) pthread_getspecific(err_reply_key);
 	if (err_reply_p == NULL) {
-		DEBUG("Error message was not allocated. %s", __func__);
+		ERROR("Error message was not allocated. %s", __func__);
 		return;
 	}
 	json_object *err_reply = *err_reply_p;
 
 	json_object *array = NULL;
 	if (err_reply == NULL) {
-		DEBUG("error calback: empty error list");
+		ERROR("error calback: empty error list");
 		pthread_mutex_lock(&json_lock);
 		err_reply = json_object_new_object();
 		array = json_object_new_array();
@@ -227,7 +227,7 @@
 		pthread_mutex_unlock(&json_lock);
 		(*err_reply_p) = err_reply;
 	} else {
-		DEBUG("error calback: nonempty error list");
+		ERROR("error calback: nonempty error list");
 		pthread_mutex_lock(&json_lock);
 		if (json_object_object_get_ex(err_reply, "errors", &array) == TRUE) {
 			if (message != NULL) {
@@ -253,13 +253,13 @@
 	struct nc_cpblts* cpblts = NULL;
 
 	if (s == NULL) {
-		DEBUG("No session given.");
+		ERROR("No session given.");
 		return;
 	}
 
 	pthread_mutex_lock(&json_lock);
 	if (s->hello_message != NULL) {
-		DEBUG("clean previous hello message");
+		ERROR("clean previous hello message");
 		//json_object_put(s->hello_message);
 		if (json_object_object_get_ex(s->hello_message, "sid", &js_tmp) == TRUE) {
 			j_old_sid = json_object_get_string(js_tmp);
@@ -297,7 +297,7 @@
 		}
 		DEBUG("%s", json_object_to_json_string(s->hello_message));
 	} else {
-		DEBUG("Session was not given.");
+		ERROR("Session was not given.");
 		json_object_object_add(s->hello_message, "type", json_object_new_int(REPLY_ERROR));
 		json_object_object_add(s->hello_message, "error-message", json_object_new_string("Invalid session identifier."));
 	}
@@ -310,11 +310,11 @@
 {
 	json_object **err_reply = calloc(1, sizeof(json_object **));
 	if (err_reply == NULL) {
-		DEBUG("Allocation of err_reply storage failed!");
+		ERROR("Allocation of err_reply storage failed!");
 		return;
 	}
 	if (pthread_setspecific(err_reply_key, err_reply) != 0) {
-		DEBUG("cannot set thread-specific value.");
+		ERROR("cannot set thread-specific value.");
 	}
 }
 
@@ -328,7 +328,7 @@
 			pthread_mutex_unlock(&json_lock);
 		}
 		if (pthread_setspecific(err_reply_key, err_reply) != 0) {
-			DEBUG("Cannot set thread-specific hash value.");
+			ERROR("Cannot set thread-specific hash value.");
 		}
 	}
 }
@@ -345,7 +345,7 @@
 		free(err_reply);
 		err_reply = NULL;
 		if (pthread_setspecific(err_reply_key, err_reply) != 0) {
-			DEBUG("Cannot set thread-specific hash value.");
+			ERROR("Cannot set thread-specific hash value.");
 		}
 	}
 }
@@ -387,7 +387,7 @@
 			session = NULL;
 			free(locked_session);
 			locked_session = NULL;
-			DEBUG("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
+			ERROR("Creating structure session_with_mutex failed %d (%s)", errno, strerror(errno));
 			return NULL;
 		}
 		locked_session->session = session;
@@ -401,7 +401,7 @@
 		if (pthread_rwlock_wrlock(&session_lock) != 0) {
 			nc_session_free(session);
 			free (locked_session);
-			DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
 			return NULL;
 		}
 		locked_session->notifications = apr_array_make(pool, NOTIFICATION_QUEUE_SIZE, sizeof(notification_t));
@@ -417,7 +417,7 @@
 		/* unlock session list */
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock (&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 
 		/* store information about session from hello message for future usage */
@@ -429,7 +429,7 @@
 		DEBUG("NETCONF session established");
 		return (session_key);
 	} else {
-		DEBUG("Connection could not be established");
+		ERROR("Connection could not be established");
 		return (NULL);
 	}
 
@@ -440,7 +440,7 @@
 	DEBUG("lock private lock.");
 	DEBUG("LOCK mutex %s", __func__);
 	if (pthread_mutex_lock(&locked_session->lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("Error while locking rwlock");
 	}
 	locked_session->ntfc_subscribed = 0;
 	locked_session->closed = 1;
@@ -452,7 +452,7 @@
 	DEBUG("unlock private lock.");
 	DEBUG("UNLOCK mutex %s", __func__);
 	if (pthread_mutex_unlock(&locked_session->lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("Error while locking rwlock");
 	}
 
 	DEBUG("unlock session lock.");
@@ -485,7 +485,7 @@
 	DEBUG("lock session lock.");
 	DEBUG("LOCK wrlock %s", __func__);
 	if (pthread_rwlock_wrlock (&session_lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("Error while locking rwlock");
 		(*reply) = create_error("Internal: Error while locking.");
 		return EXIT_FAILURE;
 	}
@@ -496,14 +496,14 @@
 
 	DEBUG("UNLOCK wrlock %s", __func__);
 	if (pthread_rwlock_unlock (&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock");
+		ERROR("Error while unlocking rwlock");
 		(*reply) = create_error("Internal: Error while unlocking.");
 	}
 
 	if ((locked_session != NULL) && (locked_session->session != NULL)) {
 		return close_and_free_session(locked_session);
 	} else {
-		DEBUG("Unknown session to close");
+		ERROR("Unknown session to close");
 		(*reply) = create_error("Internal: Unkown session to close.");
 		return (EXIT_FAILURE);
 	}
@@ -528,7 +528,7 @@
 	switch (msgt) {
 		case NC_MSG_UNKNOWN:
 			if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
-				DEBUG("mod_netconf: receiving rpc-reply failed");
+				ERROR("mod_netconf: receiving rpc-reply failed");
 				if (session_key != NULL) {
 					netconf_close(session_key, &err);
 				}
@@ -554,21 +554,21 @@
 					return create_ok();
 				case NC_REPLY_DATA:
 					if (((*data) = nc_reply_get_data(reply)) == NULL) {
-						DEBUG("mod_netconf: no data from reply");
+						ERROR("mod_netconf: no data from reply");
 						return create_error("Internal: No data from reply received.");
 					} else {
 						return NULL;
 					}
 					break;
 				case NC_REPLY_ERROR:
-					DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
+					ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
 					if (data != NULL) {
 						free(*data);
 						(*data) = NULL;
 					}
 					return create_error(nc_reply_get_errormsg(reply));
 				default:
-					DEBUG("mod_netconf: unexpected rpc-reply (%d)", replyt);
+					ERROR("mod_netconf: unexpected rpc-reply (%d)", replyt);
 					if (data != NULL) {
 						free(*data);
 						(*data) = NULL;
@@ -577,7 +577,7 @@
 			}
 			break;
 		default:
-			DEBUG("mod_netconf: unexpected reply message received (%d)", msgt);
+			ERROR("mod_netconf: unexpected reply message received (%d)", msgt);
 			if (data != NULL) {
 				free(*data);
 				(*data) = NULL;
@@ -593,7 +593,7 @@
 
 	/* check requests */
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: rpc is not created");
+		ERROR("mod_netconf: rpc is not created");
 		return create_error("Internal error: RPC is not created");
 	}
 
@@ -603,7 +603,7 @@
 		/* process the result of the operation */
 		return netconf_test_reply(session, NULL, msgt, reply, NULL);
 	} else {
-		DEBUG("Unknown session to process.");
+		ERROR("Unknown session to process.");
 		return create_error("Internal error: Unknown session to process.");
 	}
 }
@@ -627,7 +627,7 @@
 
 	/* check requests */
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: rpc is not created");
+		ERROR("mod_netconf: rpc is not created");
 		res = create_error("Internal: RPC could not be created.");
 		data = NULL;
 		goto finished;
@@ -636,7 +636,7 @@
 	/* get non-exclusive (read) access to sessions_list (conns) */
 	DEBUG("LOCK wrlock %s", __func__);
 	if (pthread_rwlock_rdlock(&session_lock) != 0) {
-		DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
 		res = create_error("Internal: Lock failed.");
 		data = NULL;
 		goto finished;
@@ -654,7 +654,7 @@
 			DEBUG("UNLOCK wrlock %s", __func__);
 			if (pthread_rwlock_unlock (&session_lock) != 0) {
 
-				DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
+				ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
 				res = create_error("Internal: Could not unlock.");
 				goto finished;
 			}
@@ -663,7 +663,7 @@
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
 
-			DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while locking rwlock: %d (%s)", errno, strerror(errno));
 			res = create_error("Internal: Could not unlock.");
 		}
 
@@ -682,9 +682,9 @@
 		/* release lock on failure */
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
-		DEBUG("Unknown session to process.");
+		ERROR("Unknown session to process.");
 		res = create_error("Unknown session to process.");
 		data = NULL;
 	}
@@ -717,7 +717,7 @@
 	rpc = nc_rpc_getconfig (source, f);
 	nc_filter_free(f);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return (NULL);
 	}
 
@@ -728,7 +728,7 @@
 	if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
 	//if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
 #endif
-		DEBUG("mod_netconf: setting withdefaults failed");
+		ERROR("mod_netconf: setting withdefaults failed");
 	}
 
 	res = netconf_op(session_key, rpc, &data);
@@ -751,7 +751,7 @@
 	/* create requests */
 	rpc = nc_rpc_getschema(identifier, version, format);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return (NULL);
 	}
 
@@ -782,7 +782,7 @@
 	rpc = nc_rpc_get (f);
 	nc_filter_free(f);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return (NULL);
 	}
 
@@ -790,7 +790,7 @@
 	if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_NOTSET)) {
 	//if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL)) {
 	//if (nc_rpc_capability_attr(rpc, NC_CAP_ATTR_WITHDEFAULTS_MODE, NCWD_MODE_ALL_TAGGED)) {
-		DEBUG("mod_netconf: setting withdefaults failed");
+		ERROR("mod_netconf: setting withdefaults failed");
 	}
 
 	res = netconf_op(session_key, rpc, &data);
@@ -836,7 +836,7 @@
 		}
 	}
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -854,7 +854,7 @@
 	/* create requests */
 	rpc = nc_rpc_editconfig(target, source, defop, erropt, testopt, config_or_url);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -872,7 +872,7 @@
 	/* create requests */
 	rpc = nc_rpc_killsession(sid);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -889,7 +889,7 @@
 	/* create requests */
 	rpc = op_func(target);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -908,7 +908,7 @@
 		rpc = nc_rpc_deleteconfig(target, url);
 	}
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -935,7 +935,7 @@
 	/* create requests */
 	rpc = nc_rpc_generic(content);
 	if (rpc == NULL) {
-		DEBUG("mod_netconf: creating rpc request failed");
+		ERROR("mod_netconf: creating rpc request failed");
 		return create_error("Internal: Creating rpc request failed");
 	}
 
@@ -957,13 +957,17 @@
 void clb_print(NC_VERB_LEVEL level, const char* msg)
 {
 #define FOREACH(I) \
-        I(NC_VERB_ERROR) I(NC_VERB_WARNING) \
-        I(NC_VERB_VERBOSE) I(NC_VERB_DEBUG)
-#define CASE(VAL) case VAL: DEBUG("%s: %s", #VAL, msg); \
+        I(NC_VERB_ERROR) I(NC_VERB_WARNING)
+
+#define CASE(VAL) case VAL: ERROR("%s: %s", #VAL, msg); \
 	break;
 
 	switch (level) {
-	FOREACH(CASE)
+	FOREACH(CASE);
+	case NC_VERB_VERBOSE:
+	case NC_VERB_DEBUG:
+		DEBUG("DEBUG: %s", msg);
+		break;
 	}
 	if (level == NC_VERB_ERROR) {
 		/* return global error */
@@ -1023,7 +1027,7 @@
 			}
 			chunk_len_str[i++] = c;
 			if (i==11) {
-				DEBUG("Message is too long, buffer for length is not big enought!!!!");
+				ERROR("Message is too long, buffer for length is not big enought!!!!");
 				break;
 			}
 		}
@@ -1159,7 +1163,7 @@
 				nc_cpblts_add(cpblts, json_object_get_string(json_object_array_get_idx(capabilities, i)));
 			}
 		} else {
-			DEBUG("no capabilities specified");
+			ERROR("no capabilities specified");
 		}
 		json_object_put(capabilities);
 	}
@@ -1168,7 +1172,7 @@
 
 	DEBUG("host: %s, port: %s, user: %s", host, port, user);
 	if ((host == NULL) || (user == NULL)) {
-		DEBUG("Cannot connect - insufficient input.");
+		ERROR("Cannot connect - insufficient input.");
 		session_key_hash = NULL;
 	} else {
 		session_key_hash = netconf_connect(pool, host, port, user, pass, cpblts);
@@ -1187,11 +1191,11 @@
 			reply = json_object_new_object();
 			json_object_object_add(reply, "type", json_object_new_int(REPLY_ERROR));
 			json_object_object_add(reply, "error-message", json_object_new_string("Connecting NETCONF server failed."));
-			DEBUG("Connection failed.");
+			ERROR("Connection failed.");
 		} else {
 			/* use filled err_reply from libnetconf's callback */
 			reply = err_reply;
-			DEBUG("Connect - error from libnetconf's callback.");
+			ERROR("Connect - error from libnetconf's callback.");
 		}
 	} else {
 		/* positive reply */
@@ -1557,7 +1561,7 @@
 
 	DEBUG("LOCK wrlock %s", __func__);
 	if (pthread_rwlock_wrlock(&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		return NULL;
 	}
 
@@ -1580,12 +1584,12 @@
 		pthread_mutex_unlock(&locked_session->lock);
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 	} else {
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 		reply = create_error("Invalid session identifier.");
 	}
@@ -1604,7 +1608,7 @@
 
 	DEBUG("LOCK wrlock %s", __func__);
 	if (pthread_rwlock_rdlock(&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 	}
 
 	locked_session = (struct session_with_mutex *) apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
@@ -1613,7 +1617,7 @@
 		pthread_mutex_lock(&locked_session->lock);
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 		if (locked_session->hello_message != NULL) {
 			reply = locked_session->hello_message;
@@ -1625,7 +1629,7 @@
 	} else {
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 		reply = create_error("Invalid session identifier.");
 	}
@@ -1638,14 +1642,14 @@
 {
 	json_object *notif_history_array = (json_object *) pthread_getspecific(notif_history_key);
 	if (notif_history_array == NULL) {
-		DEBUG("No list of notification history found.");
+		ERROR("No list of notification history found.");
 		return;
 	}
 	DEBUG("Got notification from history %lu.", (long unsigned) eventtime);
 	pthread_mutex_lock(&json_lock);
 	json_object *notif = json_object_new_object();
 	if (notif == NULL) {
-		DEBUG("Could not allocate memory for notification (json).");
+		ERROR("Could not allocate memory for notification (json).");
 		goto failed;
 	}
 	json_object_object_add(notif, "eventtime", json_object_new_int64(eventtime));
@@ -1694,7 +1698,7 @@
 
 	DEBUG("LOCK wrlock %s", __func__);
 	if (pthread_rwlock_rdlock(&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		reply = create_error("Internal lock failed.");
 		goto finalize;
 	}
@@ -1705,7 +1709,7 @@
 		pthread_mutex_lock(&locked_session->lock);
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 		DEBUG("creating temporal NC session.");
 		temp_session = nc_session_connect_channel(locked_session->session, NULL);
@@ -1739,7 +1743,7 @@
 			json_object *notif_history_array = json_object_new_array();
 			pthread_mutex_unlock(&json_lock);
 			if (pthread_setspecific(notif_history_key, notif_history_array) != 0) {
-				DEBUG("notif_history: cannot set thread-specific hash value.");
+				ERROR("notif_history: cannot set thread-specific hash value.");
 			}
 
 			ncntf_dispatch_receive(temp_session, notification_history);
@@ -1764,7 +1768,7 @@
 	} else {
 		DEBUG("UNLOCK wrlock %s", __func__);
 		if (pthread_rwlock_unlock(&session_lock) != 0) {
-			DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+			ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 		}
 		reply = create_error("Invalid session identifier.");
 	}
@@ -1864,7 +1868,6 @@
 
 		if (status == 0 || (status == -1 && (errno == EAGAIN || (errno == EINTR && isterminated == 0)))) {
 			/* poll was interrupted - check if the isterminated is set and if not, try poll again */
-			//DEBUG("poll interrupted");
 			continue;
 		} else if (status < 0) {
 			/* 0:  poll time outed
@@ -1872,7 +1875,6 @@
 			 * -1: poll failed
 			 *     something wrong happend, close this socket and wait for another request
 			 */
-			//DEBUG("poll failed, status %d(%d: %s)", status, errno, strerror(errno));
 			close(client);
 			break;
 		}
@@ -1883,7 +1885,6 @@
 		/* if nothing to read and POLLHUP (EOF) or POLLERR set */
 		if ((fds.revents & POLLHUP) || (fds.revents & POLLERR)) {
 			/* close client's socket (it's probably already closed by client */
-			//DEBUG("socket error (%d)", fds.revents);
 			close(client);
 			break;
 		}
@@ -1897,7 +1898,7 @@
 			pthread_mutex_lock(&json_lock);
 			request = json_tokener_parse_verbose(buffer, &jerr);
 			if (jerr != json_tokener_success) {
-				DEBUG("JSON parsing error");
+				ERROR("JSON parsing error");
 				pthread_mutex_unlock(&json_lock);
 				continue;
 			}
@@ -2079,7 +2080,7 @@
 				pthread_mutex_unlock(&json_lock);
 				clean_err_reply();
 			} else {
-				DEBUG("Reply is NULL, shouldn't be...");
+				ERROR("Reply is NULL, shouldn't be...");
 				continue;
 			}
 		}
@@ -2113,7 +2114,7 @@
 	/* get exclusive access to sessions_list (conns) */
 	DEBUG("LOCK wrlock %s", __func__);
 	if ((ret = pthread_rwlock_wrlock (&session_lock)) != 0) {
-		DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
+		ERROR("Error while locking rwlock: %d (%s)", ret, strerror(ret));
 		return;
 	}
 	for (hi = apr_hash_first(p, netconf_sessions_list); hi; hi = apr_hash_next(hi)) {
@@ -2134,7 +2135,7 @@
 	/* get exclusive access to sessions_list (conns) */
 	DEBUG("UNLOCK wrlock %s", __func__);
 	if (pthread_rwlock_unlock (&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 	}
 }
 
@@ -2150,7 +2151,6 @@
 	int ret;
 
 	/* get exclusive access to sessions_list (conns) */
-//DEBUG("LOCK wrlock %s", __func__);
 	if ((ret = pthread_rwlock_wrlock(&session_lock)) != 0) {
 		DEBUG("Error while locking rwlock: %d (%s)", ret, strerror(ret));
 		return;
@@ -2165,26 +2165,22 @@
 		if (ns == NULL) {
 			continue;
 		}
-//DEBUG("LOCK mutex %s", __func__);
 		pthread_mutex_lock(&swm->lock);
 		if ((current_time - swm->last_activity) > apr_time_from_sec(ACTIVITY_TIMEOUT)) {
 			DEBUG("Closing NETCONF session (%s).", hashed_key);
 			/* remove session from the active sessions list */
 			apr_hash_set(netconf_sessions_list, hashed_key, APR_HASH_KEY_STRING, NULL);
-//DEBUG("UNLOCK mutex %s", __func__);
 			pthread_mutex_unlock(&swm->lock);
 
 			/* close_and_free_session handles locking on its own */
 			close_and_free_session(swm);
 		} else {
-//DEBUG("UNLOCK mutex %s", __func__);
 			pthread_mutex_unlock(&swm->lock);
 		}
 	}
 	/* get exclusive access to sessions_list (conns) */
-//DEBUG("UNLOCK wrlock %s", __func__);
 	if (pthread_rwlock_unlock(&session_lock) != 0) {
-		DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
+		ERROR("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
 	}
 }
 
@@ -2228,12 +2224,12 @@
 	if (strlen(SU_GROUP) > 0) {
 		struct group *g = getgrnam(SU_GROUP);
 		if (g == NULL) {
-			DEBUG("GID (%s) was not found.", SU_GROUP);
+			ERROR("GID (%s) was not found.", SU_GROUP);
 			return;
 		}
 		if (setgid(g->gr_gid) != 0) {
 
-			DEBUG("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
+			ERROR("Switching to %s GID failed. (%s)", SU_GROUP, strerror(errno));
 			return;
 		}
 	}
@@ -2244,11 +2240,11 @@
 	if (strlen(SU_USER) > 0) {
 		struct passwd *p = getpwnam(SU_USER);
 		if (p == NULL) {
-			DEBUG("UID (%s) was not found.", SU_USER);
+			ERROR("UID (%s) was not found.", SU_USER);
 			return;
 		}
 		if (setuid(p->pw_uid) != 0) {
-			DEBUG("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
+			ERROR("Switching to UID %s failed. (%s)", SU_USER, strerror(errno));
 			return;
 		}
 	}
@@ -2260,7 +2256,7 @@
 	if (server != NULL) {
 		cfg = ap_get_module_config(server->module_config, &netconf_module);
 		if (cfg == NULL) {
-			DEBUG("Getting mod_netconf configuration failed");
+			ERROR("Getting mod_netconf configuration failed");
 			return;
 		}
 		sockname = cfg->sockname;
@@ -2273,7 +2269,7 @@
 
 	/* create listening UNIX socket to accept incoming connections */
 	if ((lsock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
-		DEBUG("Creating socket failed (%s)", strerror(errno));
+		ERROR("Creating socket failed (%s)", strerror(errno));
 		goto error_exit;
 	}
 
@@ -2283,15 +2279,15 @@
 
 	if (bind(lsock, (struct sockaddr *) &local, len) == -1) {
 		if (errno == EADDRINUSE) {
-			DEBUG("mod_netconf socket address already in use");
+			ERROR("mod_netconf socket address already in use");
 			goto error_exit;
 		}
-		DEBUG("Binding socket failed (%s)", strerror(errno));
+		ERROR("Binding socket failed (%s)", strerror(errno));
 		goto error_exit;
 	}
 
 	if (listen(lsock, MAX_SOCKET_CL) == -1) {
-		DEBUG("Setting up listen socket failed (%s)", strerror(errno));
+		ERROR("Setting up listen socket failed (%s)", strerror(errno));
 		goto error_exit;
 	}
 	chmod(sockname, S_IWUSR | S_IWGRP | S_IWOTH | S_IRUSR | S_IRGRP | S_IROTH);
@@ -2311,7 +2307,7 @@
 		}
 	}
 	if (chown(sockname, user, group) == -1) {
-		DEBUG("Chown on socket file failed (%s).", strerror(errno));
+		ERROR("Chown on socket file failed (%s).", strerror(errno));
 	}
 
 	/* prepare internal lists */
@@ -2319,7 +2315,7 @@
 
 	#ifdef WITH_NOTIFICATIONS
 	if (notification_init(pool, server) == -1) {
-		DEBUG("libwebsockets initialization failed");
+		ERROR("libwebsockets initialization failed");
 		use_notifications = 0;
 	} else {
 		use_notifications = 1;
@@ -2343,18 +2339,17 @@
 	pthread_rwlockattr_setpshared(&lock_attrs, PTHREAD_PROCESS_PRIVATE);
 	/* create rw lock */
 	if (pthread_rwlock_init(&session_lock, &lock_attrs) != 0) {
-		DEBUG("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
+		ERROR("Initialization of mutex failed: %d (%s)", errno, strerror(errno));
 		goto error_exit;
 	}
 	pthread_mutex_init(&ntf_history_lock, NULL);
 	pthread_mutex_init(&json_lock, NULL);
-	DEBUG("init of notif_history_key.");
+	DEBUG("Initialization of notification history.");
 	if (pthread_key_create(&notif_history_key, NULL) != 0) {
-		DEBUG("init of notif_history_key failed");
+		ERROR("Initialization of notification history failed.");
 	}
-	DEBUG("init of err_reply_key.");
 	if (pthread_key_create(&err_reply_key, NULL) != 0) {
-		DEBUG("init of err_reply_key failed");
+		ERROR("Initialization of reply key failed.");
 	}
 
 	fcntl(lsock, F_SETFL, fcntl(lsock, F_GETFL, 0) | O_NONBLOCK);
@@ -2362,9 +2357,6 @@
 		gettimeofday(&tv, NULL);
 		timediff = (unsigned int)tv.tv_sec - olds;
 		#ifdef WITH_NOTIFICATIONS
-		if (timediff > 60) {
-			DEBUG("handling notifications");
-		}
 		if (use_notifications == 1) {
 			notification_handle();
 		}
@@ -2375,10 +2367,6 @@
 
 		/* open incoming connection if any */
 		len = sizeof(remote);
-		if (((unsigned int)tv.tv_sec - olds) > 60) {
-			DEBUG("accepting another client");
-			olds = tv.tv_sec;
-		}
 		client = accept(lsock, (struct sockaddr *) &remote, &len);
 		if (client == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
 			apr_sleep(SLEEP_TIME);
@@ -2386,7 +2374,7 @@
 		} else if (client == -1 && (errno == EINTR)) {
 			continue;
 		} else if (client == -1) {
-			DEBUG("Accepting mod_netconf client connection failed (%s)", strerror(errno));
+			ERROR("Accepting mod_netconf client connection failed (%s)", strerror(errno));
 			continue;
 		}
 
@@ -2401,7 +2389,7 @@
 
 		/* start new thread. It will serve this particular request and then terminate */
 		if ((ret = pthread_create (&ptids[pthread_count], NULL, thread_routine, (void*)arg)) != 0) {
-			DEBUG("Creating POSIX thread failed: %d\n", ret);
+			ERROR("Creating POSIX thread failed: %d\n", ret);
 		} else {
 			DEBUG("Thread %lu created", ptids[pthread_count]);
 			pthread_count++;
@@ -2511,11 +2499,11 @@
 			apr_pool_note_subprocess(config->pool, config->forkproc, APR_KILL_AFTER_TIMEOUT);
 			break;
 		default:
-			DEBUG("apr_proc_fork() failed");
+			ERROR("apr_proc_fork() failed");
 			break;
 		}
 	} else {
-		DEBUG("mod_netconf misses configuration structure");
+		ERROR("mod_netconf misses configuration structure");
 	}
 
 	return OK;
diff --git a/src/mod_netconf.h b/src/mod_netconf.h
index 1997171..2e8951a 100644
--- a/src/mod_netconf.h
+++ b/src/mod_netconf.h
@@ -98,12 +98,24 @@
 
 extern server_rec *http_server;
 #ifndef HTTPD_INDEPENDENT
+# define APLOGDEBUG(...) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, __VA_ARGS__);
 # define APLOGERROR(...) ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, __VA_ARGS__);
 #else
+# define APLOGDEBUG(...)
 # define APLOGERROR(...)
 #endif
+
 #define DEBUG(...) do { \
 	if (http_server != NULL) { \
+		APLOGDEBUG(__VA_ARGS__); \
+	} else { \
+		fprintf(stderr, __VA_ARGS__); \
+		fprintf(stderr, "\n"); \
+	} \
+} while (0);
+
+#define ERROR(...) do { \
+	if (http_server != NULL) { \
 		APLOGERROR(__VA_ARGS__); \
 	} else { \
 		fprintf(stderr, __VA_ARGS__); \
diff --git a/src/notification-server.c b/src/notification-server.c
index ba039b2..5ff3b5f 100644
--- a/src/notification-server.c
+++ b/src/notification-server.c
@@ -121,68 +121,12 @@
 	char client_ip[128];
 	char buf[256];
 	int n, m;
-	unsigned char *p;
 	static unsigned char buffer[4096];
-	struct stat stat_buf;
 	struct per_session_data__http *pss = (struct per_session_data__http *)user;
 	struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in;
 
 	switch (reason) {
 	case LWS_CALLBACK_HTTP:
-
-		/* check for the "send a big file by hand" example case */
-
-		if (!strcmp((const char *)in, "/leaf.jpg")) {
-			char leaf_path[1024];
-			snprintf(leaf_path, sizeof(leaf_path), "%s/leaf.jpg", resource_path);
-
-			/* well, let's demonstrate how to send the hard way */
-
-			p = buffer;
-
-			pss->fd = open(leaf_path, O_RDONLY);
-
-			if (pss->fd < 0)
-				return -1;
-
-			fstat(pss->fd, &stat_buf);
-
-			/*
-			 * we will send a big jpeg file, but it could be
-			 * anything.  Set the Content-Type: appropriately
-			 * so the browser knows what to do with it.
-			 */
-
-			p += sprintf((char *)p,
-				"HTTP/1.0 200 OK\x0d\x0a"
-				"Server: libwebsockets\x0d\x0a"
-				"Content-Type: image/jpeg\x0d\x0a"
-					"Content-Length: %u\x0d\x0a\x0d\x0a",
-					(unsigned int)stat_buf.st_size);
-
-			/*
-			 * send the http headers...
-			 * this won't block since it's the first payload sent
-			 * on the connection since it was established
-			 * (too small for partial)
-			 */
-
-			n = libwebsocket_write(wsi, buffer,
-				   p - buffer, LWS_WRITE_HTTP);
-
-			if (n < 0) {
-				close(pss->fd);
-				return -1;
-			}
-			/*
-			 * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
-			 */
-			libwebsocket_callback_on_writable(context, wsi);
-			break;
-		}
-
-		/* if not, send a file the easy way */
-
 		for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
 			if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
 				break;
@@ -306,51 +250,6 @@
 	return 0;
 }
 
-/**
- * this is just an example of parsing handshake headers, you don't need this
- * in your code unless you will filter allowing connections by the header
- * content
- */
-//static void dump_handshake_info(struct libwebsocket *wsi)
-//{
-//	int n;
-//	static const char *token_names[WSI_TOKEN_COUNT] = {
-//		/*[WSI_TOKEN_GET_URI]		=*/ "GET URI",
-//		/*[WSI_TOKEN_HOST]		=*/ "Host",
-//		/*[WSI_TOKEN_CONNECTION]	=*/ "Connection",
-//		/*[WSI_TOKEN_KEY1]		=*/ "key 1",
-//		/*[WSI_TOKEN_KEY2]		=*/ "key 2",
-//		/*[WSI_TOKEN_PROTOCOL]		=*/ "Protocol",
-//		/*[WSI_TOKEN_UPGRADE]		=*/ "Upgrade",
-//		/*[WSI_TOKEN_ORIGIN]		=*/ "Origin",
-//		/*[WSI_TOKEN_DRAFT]		=*/ "Draft",
-//		/*[WSI_TOKEN_CHALLENGE]		=*/ "Challenge",
-//
-//		/* new for 04 */
-//		/*[WSI_TOKEN_KEY]		=*/ "Key",
-//		/*[WSI_TOKEN_VERSION]		=*/ "Version",
-//		/*[WSI_TOKEN_SWORIGIN]		=*/ "Sworigin",
-//
-//		/* new for 05 */
-//		/*[WSI_TOKEN_EXTENSIONS]	=*/ "Extensions",
-//
-//		/* client receives these */
-//		/*[WSI_TOKEN_ACCEPT]		=*/ "Accept",
-//		/*[WSI_TOKEN_NONCE]		=*/ "Nonce",
-//		/*[WSI_TOKEN_HTTP]		=*/ "Http",
-//		/*[WSI_TOKEN_MUXURL]	=*/ "MuxURL",
-//	};
-//	char buf[256];
-//
-//	for (n = 0; n < WSI_TOKEN_COUNT; n++) {
-//		if (!lws_hdr_total_length(wsi, n))
-//			continue;
-//
-//		//lws_hdr_copy(wsi, buf, sizeof buf, n);
-//
-//		//fprintf(stderr, "    %s = %s\n", token_names[n], buf);
-//	}
-//}
 
 /* dumb_increment protocol */
 
@@ -389,12 +288,12 @@
 	switch (nc_session_send_recv(session, rpc, &reply)) {
 	case NC_MSG_UNKNOWN:
 		if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
-			DEBUG("notifications: receiving rpc-reply failed.");
+			ERROR("notifications: receiving rpc-reply failed.");
 			//cmd_disconnect(NULL);
 			ret = EXIT_FAILURE;
 			break;
 		}
-		DEBUG("notifications: Unknown error occurred.");
+		ERROR("notifications: Unknown error occurred.");
 		ret = EXIT_FAILURE;
 		break;
 	case NC_MSG_NONE:
@@ -446,27 +345,27 @@
 	session_hash = pthread_getspecific(thread_key);
 	DEBUG("notification: fileprint getspecific (%s)", session_hash);
 	if (pthread_rwlock_wrlock(&session_lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("notifications: Error while locking rwlock");
 		return;
 	}
 	DEBUG("Get session with mutex from key %s.", session_hash);
 	target_session = get_ncsession_from_key(session_hash);
 	if (target_session == NULL) {
-		DEBUG("no session found last_session_key (%s)", session_hash);
+		ERROR("notifications: no session found last_session_key (%s)", session_hash);
 		goto unlock_glob;
 	}
 	if (pthread_mutex_lock(&target_session->lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("notifications: Error while locking rwlock");
 	}
 
 	if (target_session->notifications == NULL) {
-		DEBUG("target_session->notifications is NULL");
+		ERROR("notifications: target_session->notifications is NULL");
 		goto unlock_all;
 	}
 	DEBUG("notification: ready to push to notifications queue");
 	ntf = (notification_t *) apr_array_push(target_session->notifications);
 	if (ntf == NULL) {
-		DEBUG("Failed to allocate element ");
+		ERROR("notifications: Failed to allocate element ");
 		goto unlock_all;
 	}
 	ntf->eventtime = eventtime;
@@ -476,11 +375,11 @@
 
 unlock_all:
 	if (pthread_mutex_unlock(&target_session->lock) != 0) {
-		DEBUG("Error while unlocking rwlock");
+		ERROR("notifications: Error while unlocking rwlock");
 	}
 unlock_glob:
 	if (pthread_rwlock_unlock(&session_lock) != 0) {
-		DEBUG("Error while locking rwlock");
+		ERROR("notifications: Error while locking rwlock");
 	}
 }
 
@@ -495,7 +394,7 @@
 
 	/* store hash identification of netconf session for notifications printing callback */
 	if (pthread_setspecific(thread_key, config->session_hash) != 0) {
-		DEBUG("notifications: cannot set thread-specific hash value.");
+		ERROR("notifications: cannot set thread-specific hash value.");
 	}
 
 	DEBUG("notifications: dispatching");
@@ -525,7 +424,7 @@
 	DEBUG("notif_subscribe");
 	if (locked_session == NULL) {
 		DEBUG("notifications: no locked_session was given.");
-		DEBUG("Close notification client");
+		/* Close notification client */
 		return -1;
 	}
 
@@ -539,18 +438,18 @@
 	DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
 
 	if (session == NULL) {
-		DEBUG("notifications: NETCONF session not established.");
+		ERROR("notifications: NETCONF session not established.");
 		goto operation_failed;
 	}
 
 	/* check if notifications are allowed on this session */
 	if (nc_session_notif_allowed(session) == 0) {
-		DEBUG("notifications: Notification subscription is not allowed on this session.");
+		ERROR("notifications: Notification subscription is not allowed on this session.");
 		goto operation_failed;
 	}
 	/* check times */
 	if (start != -1 && stop != -1 && start > stop) {
-		DEBUG("notifications: Subscription start time must be lower than the end time.");
+		ERROR("notifications: Subscription start time must be lower than the end time.");
 		goto operation_failed;
 	}
 
@@ -559,21 +458,21 @@
 	rpc = nc_rpc_subscribe(stream, filter, (start_time == -1)?NULL:&start, (stop_time == 0)?NULL:&stop);
 	nc_filter_free(filter);
 	if (rpc == NULL) {
-		DEBUG("notifications: creating an rpc request failed.");
+		ERROR("notifications: creating an rpc request failed.");
 		goto operation_failed;
 	}
 
 	DEBUG("Send NC subscribe.");
 	create_err_reply_p();
 	if (send_recv_process(session, "subscribe", rpc) != 0) {
-		DEBUG("Subscription RPC failed.");
+		ERROR("Subscription RPC failed.");
 		goto operation_failed;
 	}
 
 	GETSPEC_ERR_REPLY
 	if (err_reply != NULL) {
                 free_err_reply();
-                DEBUG("RPC-Error received and cleaned, because we can't send it anywhere.");
+                ERROR("RPC-Error received and cleaned, because we can't send it anywhere.");
                 goto operation_failed;
 	}
 
@@ -582,6 +481,10 @@
 
 	DEBUG("Create config for notification_thread.");
 	tconfig = malloc(sizeof(struct ntf_thread_config));
+	if (tconfig == NULL) {
+		ERROR("notifications: Allocation failed.");
+		goto operation_failed;
+	}
 	tconfig->session = session;
 	tconfig->session_hash = strdup(session_hash);
 	DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_hash);
@@ -589,7 +492,7 @@
 	pthread_mutex_unlock(&locked_session->lock);
 	DEBUG("Create notification_thread.");
 	if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
-		DEBUG("notifications: creating a thread for receiving notifications failed");
+		ERROR("notifications: creating a thread for receiving notifications failed");
 		return -1;
 	}
 	pthread_detach(thread);
@@ -844,7 +747,7 @@
 	pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
 	fd_lookup = malloc(max_poll_elements * sizeof (int));
 	if (pollfds == NULL || fd_lookup == NULL) {
-		DEBUG("Out of memory pollfds=%d\n", max_poll_elements);
+		ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
 		return -1;
 	}
 
@@ -868,9 +771,8 @@
 		return -1;
 	}
 
-	DEBUG("notifications: init of pthread_key_create.");
 	if (pthread_key_create(&thread_key, NULL) != 0) {
-		DEBUG("notifications: pthread_key_create failed");
+		ERROR("notifications: pthread_key_create failed");
 	}
 	return 0;
 }