diff --git a/ChangeLog b/ChangeLog
index 3e90921ef593d32fe4910866410f7ef228acfb97..5c7fa35ee87513255c4e2b082ec3fea52198373b 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2005-11-01  Kevin P. Fleming  <kpfleming@digium.com>
 
+	* many files: make chan_misdn actually build (issue #5566)
+
 	* many files: more Cygwin build system support (issue #4678)
 
 	* apps/app_parkandannounce.c (parkandannounce_exec): supply parent channel to ast_request_and_dial so channel variables can be inherited (issue #5564)
diff --git a/channels/chan_misdn.c b/channels/chan_misdn.c
index 1561e558d3988650b3e1d33ef7c67b94a9bc4a6c..b7ad715910c4cb20745df6d7d0db49931fecdd5d 100755
--- a/channels/chan_misdn.c
+++ b/channels/chan_misdn.c
@@ -47,7 +47,7 @@
 #include "chan_misdn_config.h"
 #include "isdn_lib.h"
 
-pthread_mutex_t release_lock_mutex;
+ast_mutex_t release_lock_mutex;
 
 #define release_lock ast_mutex_lock(&release_lock_mutex)
 #define release_unlock ast_mutex_unlock(&release_lock_mutex)
@@ -81,8 +81,7 @@ enum misdn_chan_state {
 
 struct chan_list {
   
-	sem_t sem;
-	pthread_mutex_t lock;
+	ast_mutex_t lock;
 
 	pthread_t *audio_thread;
   
@@ -169,6 +168,10 @@ void send_digit_to_chan(struct chan_list *cl, char digit );
 #define AST_LOAD_CFG ast_config_load
 #define AST_DESTROY_CFG ast_config_destroy
 
+#define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
+#define MISDN_ASTERISK_PVT(ast) 1
+#define MISDN_ASTERISK_TYPE(ast) ast->tech->type
+
 /* END: chan_misdn.h */
 
 #include <asterisk/strings.h>
@@ -197,7 +200,7 @@ int max_ports;
 struct chan_list dummy_cl;
 
 struct chan_list *cl_te=NULL;
-pthread_mutex_t cl_te_lock;
+ast_mutex_t cl_te_lock;
 
 enum event_response_e
 cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data);
@@ -292,15 +295,15 @@ static char *bearer2str(int cap) {
 
 static void print_bearer(struct misdn_bchannel *bc) 
 {
-
-	chan_misdn_log(2, bc->stack->port, " --> Bearer: %s\n",bearer2str(bc->capability));
+	
+	chan_misdn_log(2, bc->port, " --> Bearer: %s\n",bearer2str(bc->capability));
 	
 	switch(bc->law) {
 	case INFO_CODEC_ALAW:
-		chan_misdn_log(2, bc->stack->port, " --> Codec: Alaw\n");
+		chan_misdn_log(2, bc->port, " --> Codec: Alaw\n");
 		break;
 	case INFO_CODEC_ULAW:
-		chan_misdn_log(2, bc->stack->port, " --> Codec: Ulaw\n");
+		chan_misdn_log(2, bc->port, " --> Codec: Ulaw\n");
 		break;
 	}
 }
@@ -408,30 +411,6 @@ static int misdn_set_crypt_debug(int fd, int argc, char *argv[])
 	return 0;
 }
 
-static int misdn_flush_stack (int fd, int argc, char *argv[])
-{
-	int port, i;
-	struct misdn_stack *stack = get_misdn_stack();
-  
-	if (argc != 4)
-		return RESULT_SHOWUSAGE;
-  
-	port = atoi(argv[3]);
-
-	for (;
-	     stack;
-	     stack=stack->next ) {
-		if (stack->port == port) {
-			for (i=0; i< stack->b_num; i++) {
-				struct misdn_bchannel *mybc=&stack->bc[i];
-				mybc->in_use=0;
-			}
-		}
-	}
-  
-  
-	return 0;
-}
 
 static int misdn_restart_port (int fd, int argc, char *argv[])
 {
@@ -586,21 +565,22 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
 	struct ast_channel *ast=help->ast;
 	ast_cli(fd,
 		"* Pid:%d Prt:%d Ch:%d Mode:%s Org:%s dad:%s oad:%s ctx:%s state:%s\n",
-		bc->pid, bc->stack->port, bc->channel,
-		bc->stack->mode==NT_MODE?"NT":"TE",
+		bc->pid, bc->port, bc->channel,
+		bc->nt?"NT":"TE",
 		help->orginator == ORG_AST?"*":"I",
 		ast?ast->exten:NULL,
 		ast?AST_CID_P(ast):NULL,
 		ast?ast->context:NULL,
 		misdn_get_ch_state(help)
 		);
-	if (misdn_debug[bc->stack->port] > 0)
+	if (misdn_debug[bc->port] > 0)
 		ast_cli(fd,
 			"  --> astname: %s\n"
 			"  --> ch_l3id: %x\n"
 			"  --> ch_addr: %x\n"
 			"  --> bc_addr: %x\n"
 			"  --> bc_l3id: %x\n"
+			"  --> tone: %s\n"
 			"  --> display: %s\n"
 			"  --> activated: %d\n"
 			"  --> capability: %s\n"
@@ -612,6 +592,7 @@ static void print_bc_info (int fd, struct chan_list* help, struct misdn_bchannel
 			help->addr,
 			bc->addr,
 			bc?bc->l3_id:-1,
+			tone2str(bc),
 			bc->display,
 			
 			bc->active,
@@ -673,7 +654,7 @@ static int misdn_show_cl (int fd, int argc, char *argv[])
 	return 0;
 }
 
-pthread_mutex_t lock;
+ast_mutex_t lock;
 int MAXTICS=8;
 
 static int misdn_set_tics (int fd, int argc, char *argv[])
@@ -687,36 +668,19 @@ static int misdn_set_tics (int fd, int argc, char *argv[])
 }
 
 
-static int misdn_show_fullstacks (int fd, int argc, char *argv[])
-{
-	struct misdn_stack *stack = get_misdn_stack();
-	ast_cli(fd, "BEGIN STACK_LIST:\n");
-	for (;
-		stack;
-		stack=stack->next ) {
-		int i;
-		ast_cli(fd, "* Stack Addr: Uid %x Port %d Type %s Prot. %s Link %s\n",stack->upper_id, stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN");
-		for (i=0; i< stack->b_num; i++) {
-			struct misdn_bchannel *mybc=&stack->bc[i];
-			ast_cli(fd," --> bchan: addr %x channel %d pid %d cr %x tone %s inuse %d\n", mybc->addr,mybc->channel, mybc?mybc->pid:-1,mybc?mybc->l3_id:-1 , tone2str(mybc), mybc->in_use);
-		}
-	}
-
-	return 0;
-}
 
 static int misdn_show_stacks (int fd, int argc, char *argv[])
 {
-	struct misdn_stack *stack = get_misdn_stack();
-	int i=1;
+	int port;
 	
 	ast_cli(fd, "BEGIN STACK_LIST:\n");
-	for (;
-	     stack;
-	     stack=stack->next ) {
-		ast_cli(fd, "* Stack Addr: Port %d Type %s Prot. %s L2Link %s L1Link:%s Debug:%d%s\n", stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN", misdn_debug[i], misdn_debug_only[i]?"(only)":"");
-		i++;
+	for (port=misdn_cfg_get_next_port(0); port > 0;
+	     port=misdn_cfg_get_next_port(port)) {
+		char buf[128];
+		get_show_stack_details(port,buf);
+		ast_cli(fd,"  %s  Debug:%d%s\n", buf, misdn_debug[port], misdn_debug_only[port]?"(only)":"");
 	}
+		
 
 	return 0;
 
@@ -724,27 +688,20 @@ static int misdn_show_stacks (int fd, int argc, char *argv[])
 
 static int misdn_show_port (int fd, int argc, char *argv[])
 {
-	struct misdn_stack *stack = get_misdn_stack();
-	int i;
 	int port;
-  
+	
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
   
 	port = atoi(argv[3]);
   
 	ast_cli(fd, "BEGIN STACK_LIST:\n");
-	for (;
-	     stack;
-	     stack=stack->next ) {
-		if (stack->port == port) {
-			ast_cli(fd, "* Stack Addr: %x Port %d Type %s Prot. %s L2Link %s L1Link:%s\n",stack->upper_id, stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
-			for (i=0; i <stack->b_num; i++) {
-				ast_cli(fd,"Idx: %d stack->chan: %d Chan %d InUse:%d\n",i,stack->channels[i], i+1, stack->bc[i].in_use);
-			}
-		}
-	}
 
+	char buf[128];
+	get_show_stack_details(port,buf);
+	ast_cli(fd,"  %s  Debug:%d%s\n",buf, misdn_debug[port], misdn_debug_only[port]?"(only)":"");
+
+	
 	return 0;
 }
 
@@ -1010,19 +967,7 @@ static struct ast_cli_entry cli_show_cl =
   complete_ch
 };
 
-static struct ast_cli_entry cli_show_fullstacks =
-{ {"misdn","show","fullstacks", NULL},
-  misdn_show_fullstacks,
-  "Shows internal mISDN stack_list with bchannels", 
-  "Usage: misdn show fullstacks\n"
-};
 
-static struct ast_cli_entry cli_flush_stack =
-{ {"misdn","flush","stack", NULL},
-  misdn_flush_stack,
-  "Flushes the in_use flag", 
-  "Usage: misdn flush stack\n"
-};
 
 static struct ast_cli_entry cli_restart_port =
 { {"misdn","restart","port", NULL},
@@ -1127,11 +1072,11 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 		return -1;
 	}
 	
-	port=newbc->stack->port;
+	port=newbc->port;
+
 	
 	chan_misdn_log(1, 0, "* CALL: %s\n",dest);
 	
-
 	chan_misdn_log(1, port, " --> * dad:%s tech:%s ctx:%s\n",ast->exten,ast->name, ast->context);
 	
 	{
@@ -1149,8 +1094,8 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 		misdn_cfg_get( port, MISDN_CFG_TXGAIN, &newbc->txgain, sizeof(int));
 		misdn_cfg_get( port, MISDN_CFG_RXGAIN, &newbc->rxgain, sizeof(int));
 		
-		misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(newbc->stack->te_choose_channel), sizeof(int));
-
+		misdn_cfg_get( port, MISDN_CFG_TE_CHOOSE_CHANNEL, &(newbc->te_choose_channel), sizeof(int));
+		
 
 		{
 			char callerid[BUFFERSIZE];
@@ -1179,19 +1124,6 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 			}
 		}
 		
-		
-		{
-			char buf[256];
-			ast_group_t pg,cg;
-
-			misdn_cfg_get( port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
-			misdn_cfg_get( port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
-			
-			chan_misdn_log(2, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
-			ast->pickupgroup=pg;
-			ast->callgroup=cg;
-		}
-
 		/* Will be overridden by asterisk in head! */
 		{
 			int pres;
@@ -1202,7 +1134,7 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 		}
 		
 		int def_callingpres;
-		misdn_cfg_get( 0, MISDN_CFG_USE_CALLINGPRES, &def_callingpres, sizeof(int));
+		misdn_cfg_get( port, MISDN_CFG_USE_CALLINGPRES, &def_callingpres, sizeof(int));
 		if ( def_callingpres) {
 			switch (ast->cid.cid_pres){
 			case AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED:
@@ -1238,9 +1170,6 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 		
 	} 
 	
-	
-	
-	
 	chan_misdn_log(3, port, " --> * adding2newbc ext %s\n",ast->exten);
 	if (ast->exten) {
 		int l = sizeof(newbc->dad);
@@ -1276,20 +1205,11 @@ static int misdn_call(struct ast_channel *ast, char *dest, int timeout)
 		
 
 		/* Finally The Options Override Everything */
-		if (opts) misdn_set_opt_exec(ast,opts);
+		if (opts)
+			misdn_set_opt_exec(ast,opts);
 		else
 			chan_misdn_log(1,0,"NO OPTS GIVEN\n");
 		
-/*		switch (newbc->pres) {
-		case 0:
-			chan_misdn_log(1, port, " --> Number Screened\n");
-			break;
-		case 1:
-			chan_misdn_log(1, port, " --> Number Not Screened\n");
-			break;
-		default:
-			chan_misdn_log(1, port, " --> Other Screened\n");
-		}*/
 		
 		cl_queue_chan(&cl_te, ch) ;
 		ch->state=MISDN_CALLING;
@@ -1329,7 +1249,7 @@ int misdn_answer(struct ast_channel *ast)
 	
 	chan_misdn_trace_call(ast,1,"*->I: EVENT_ANSWER\n");
 	
-	chan_misdn_log(1, p? (p->bc? p->bc->stack->port : 0) : 0, "* ANSWER:\n");
+	chan_misdn_log(1, p? (p->bc? p->bc->port : 0) : 0, "* ANSWER:\n");
 	
 	if (!p) {
 		ast_log(LOG_WARNING, " --> Channel not connected ??\n");
@@ -1346,14 +1266,14 @@ int misdn_answer(struct ast_channel *ast)
 		char *tmp_key = pbx_builtin_getvar_helper(p->ast, "CRYPT_KEY");
 		
 		if (tmp_key ) {
-			chan_misdn_log(1, p->bc->stack->port, " --> Connection will be BF crypted\n");
+			chan_misdn_log(1, p->bc->port, " --> Connection will be BF crypted\n");
 			{
 				int l = sizeof(p->bc->crypt_key);
 				strncpy(p->bc->crypt_key,tmp_key, l);
 				p->bc->crypt_key[l-1] = 0;
 			}
 		} else {
-			chan_misdn_log(3, p->bc->stack->port, " --> Connection is without BF encryption\n");
+			chan_misdn_log(3, p->bc->port, " --> Connection is without BF encryption\n");
 		}
     
 	}
@@ -1375,7 +1295,7 @@ int misdn_digit(struct ast_channel *ast, char digit )
 	
 
 	struct misdn_bchannel *bc=p->bc;
-	chan_misdn_log(1, bc?bc->stack->port:0, "* IND : Digit %c\n",digit);
+	chan_misdn_log(1, bc?bc->port:0, "* IND : Digit %c\n",digit);
 	
 	if (!bc) {
 		ast_log(LOG_WARNING, " --> !! Got Digit Event withut having bchannel Object\n");
@@ -1433,7 +1353,7 @@ int misdn_fixup(struct ast_channel *oldast, struct ast_channel *ast)
 	if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
 	p = MISDN_ASTERISK_TECH_PVT(ast) ;
 	
-	chan_misdn_log(1, p->bc?p->bc->stack->port:0, "* IND: Got Fixup State:%s Holded:%d L3id:%x\n", misdn_get_ch_state(p), p->holded, p->l3id);
+	chan_misdn_log(1, p->bc?p->bc->port:0, "* IND: Got Fixup State:%s Holded:%d L3id:%x\n", misdn_get_ch_state(p), p->holded, p->l3id);
 	
 	p->ast = ast ;
 	p->state=MISDN_CONNECTED;
@@ -1449,7 +1369,7 @@ int misdn_transfer (struct ast_channel *ast, char *dest)
 	if (!ast || ! MISDN_ASTERISK_PVT(ast)) return -1;
 	p = MISDN_ASTERISK_TECH_PVT(ast) ;
 
-	chan_misdn_log(1, p->bc?p->bc->stack->port:0, "* IND : Got Transfer %s\n",dest);
+	chan_misdn_log(1, p->bc?p->bc->port:0, "* IND : Got Transfer %s\n",dest);
 	return 0;
 }
 
@@ -1472,12 +1392,12 @@ int misdn_indication(struct ast_channel *ast, int cond)
 		return -1;
 	}
 	
-	chan_misdn_log(1, p->bc->stack->port, "* IND : Indication from %s\n",ast->exten);
+	chan_misdn_log(1, p->bc->port, "* IND : Indication from %s\n",ast->exten);
 	
 	switch (cond) {
 	case AST_CONTROL_BUSY:
-		chan_misdn_log(1, p->bc->stack->port, "* IND :\tbusy\n");
-		chan_misdn_log(1, p->bc->stack->port, " --> * SEND: State Busy pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, "* IND :\tbusy\n");
+		chan_misdn_log(1, p->bc->port, " --> * SEND: State Busy pid:%d\n",p->bc?p->bc->pid:-1);
 		ast_setstate(ast,AST_STATE_BUSY);
 		
 		p->bc->out_cause=17;
@@ -1485,46 +1405,46 @@ int misdn_indication(struct ast_channel *ast, int cond)
 			misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
 			manager_send_tone(p->bc, TONE_BUSY);
 		} else {
-			chan_misdn_log(0, p->bc->stack->port, " --> !! Got Busy in Connected State !?! port:%d ast:%s\n",
-				       p->bc->stack->port, ast->name);
+			chan_misdn_log(0, p->bc->port, " --> !! Got Busy in Connected State !?! port:%d ast:%s\n",
+				       p->bc->port, ast->name);
 		}
 		break;
 	case AST_CONTROL_RING:
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tring pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\tring pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_RINGING:
 		if ( p->state == MISDN_ALERTING) {
-			chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tringing pid:%d but I ws Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1);
+			chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d but I ws Ringing before, so ignoreing it\n",p->bc?p->bc->pid:-1);
 			break;
 		}
 		p->state=MISDN_ALERTING;
 		
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\tringing pid:%d\n",p->bc?p->bc->pid:-1);
 		
 		misdn_lib_send_event( p->bc, EVENT_ALERTING);
 		
 		manager_send_tone(p->bc, TONE_ALERTING);
-		chan_misdn_log(1, p->bc->stack->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * SEND: State Ring pid:%d\n",p->bc?p->bc->pid:-1);
 		ast_setstate(ast,AST_STATE_RINGING);
 		break;
 		
 	case AST_CONTROL_ANSWER:
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tanswer pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\tanswer pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_TAKEOFFHOOK:
-		chan_misdn_log(1, p->bc->stack->port, " --> *\ttakeoffhook pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> *\ttakeoffhook pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_OFFHOOK:
-		chan_misdn_log(1, p->bc->stack->port, " --> *\toffhook pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> *\toffhook pid:%d\n",p->bc?p->bc->pid:-1);
 		break; 
 	case AST_CONTROL_FLASH:
-		chan_misdn_log(1, p->bc->stack->port, " --> *\tflash pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> *\tflash pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_PROGRESS:
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\tprogress pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_CONGESTION:
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\tcongestion pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\tcongestion pid:%d\n",p->bc?p->bc->pid:-1);
 
 		p->bc->out_cause=42;
 		if (p->state != MISDN_CONNECTED) {
@@ -1534,18 +1454,18 @@ int misdn_indication(struct ast_channel *ast, int cond)
 		} else {
 			misdn_lib_send_event( p->bc, EVENT_DISCONNECT);
 		}
-		if (p->bc->stack->mode == NT_MODE) {
+		if (p->bc->nt) {
 			manager_send_tone(p->bc, TONE_BUSY);
 		}
 		break;
 	case -1 :
-		chan_misdn_log(1, p->bc->stack->port, " --> * IND :\t-1! pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> * IND :\t-1! pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_HOLD:
-		chan_misdn_log(1, p->bc->stack->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> *\tHOLD pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	case AST_CONTROL_UNHOLD:
-		chan_misdn_log(1, p->bc->stack->port, " --> *\tUNHOLD pid:%d\n",p->bc?p->bc->pid:-1);
+		chan_misdn_log(1, p->bc->port, " --> *\tUNHOLD pid:%d\n",p->bc?p->bc->pid:-1);
 		break;
 	default:
 		ast_log(LOG_WARNING, " --> * Unknown Indication:%d pid:%d\n",cond,p->bc?p->bc->pid:-1);
@@ -1614,10 +1534,10 @@ int misdn_hangup(struct ast_channel *ast)
 			bc->out_cause=tmpcause?tmpcause:16;
 		}
     
-		chan_misdn_log(1, bc->stack->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",p->bc?p->bc->pid:-1, ast->context, ast->exten, AST_CID_P(ast), misdn_get_ch_state(p));
-		chan_misdn_log(2, bc->stack->port, " --> l3id:%x\n",p->l3id);
-		chan_misdn_log(1, bc->stack->port, " --> cause:%d\n",bc->cause);
-		chan_misdn_log(1, bc->stack->port, " --> out_cause:%d\n",bc->out_cause);
+		chan_misdn_log(1, bc->port, "* IND : HANGUP\tpid:%d ctx:%s dad:%s oad:%s State:%s\n",p->bc?p->bc->pid:-1, ast->context, ast->exten, AST_CID_P(ast), misdn_get_ch_state(p));
+		chan_misdn_log(2, bc->port, " --> l3id:%x\n",p->l3id);
+		chan_misdn_log(1, bc->port, " --> cause:%d\n",bc->cause);
+		chan_misdn_log(1, bc->port, " --> out_cause:%d\n",bc->out_cause);
 		
 		switch (p->state) {
 		case MISDN_CALLING:
@@ -1635,7 +1555,7 @@ int misdn_hangup(struct ast_channel *ast)
 			break;
       
 		case MISDN_ALERTING:
-			chan_misdn_log(2, bc->stack->port, " --> * State Alerting\n");
+			chan_misdn_log(2, bc->port, " --> * State Alerting\n");
 
 			if (p->orginator != ORG_AST) 
 				manager_send_tone(bc, TONE_BUSY);
@@ -1645,7 +1565,7 @@ int misdn_hangup(struct ast_channel *ast)
 			break;
 		case MISDN_CONNECTED:
 			/*  Alerting or Disconect */
-			chan_misdn_log(2, bc->stack->port, " --> * State Connected\n");
+			chan_misdn_log(2, bc->port, " --> * State Connected\n");
 			start_bc_tones(p);
 			manager_send_tone(bc, TONE_BUSY);
 			misdn_lib_send_event( bc, EVENT_DISCONNECT);
@@ -1658,15 +1578,15 @@ int misdn_hangup(struct ast_channel *ast)
       
 		case MISDN_HOLD_DISCONNECT:
 			/* need to send release here */
-			chan_misdn_log(2, bc->stack->port, " --> state HOLD_DISC\n");
-			chan_misdn_log(1, bc->stack->port, " --> cause %d\n",bc->cause);
-			chan_misdn_log(1, bc->stack->port, " --> out_cause %d\n",bc->out_cause);
+			chan_misdn_log(2, bc->port, " --> state HOLD_DISC\n");
+			chan_misdn_log(1, bc->port, " --> cause %d\n",bc->cause);
+			chan_misdn_log(1, bc->port, " --> out_cause %d\n",bc->out_cause);
 			
 			misdn_lib_send_event(bc,EVENT_RELEASE);
 			break;
 		default:
 			/*  Alerting or Disconect */
-			if (bc->stack->mode == NT_MODE)
+			if (bc->nt)
 				misdn_lib_send_event(bc, EVENT_RELEASE);
 			else
 				misdn_lib_send_event(bc, EVENT_DISCONNECT);
@@ -1675,7 +1595,7 @@ int misdn_hangup(struct ast_channel *ast)
     
 	}
 	
-	chan_misdn_log(1, bc->stack->port, "Channel: %s hanguped\n",ast->name);
+	chan_misdn_log(1, bc->port, "Channel: %s hanguped\n",ast->name);
 	
 	return 0;
 }
@@ -1695,12 +1615,13 @@ struct ast_frame  *misdn_read(struct ast_channel *ast)
 	
 	read(tmp->pipe[0],blah,sizeof(blah));
 	
-	len = ibuf_usedcount(tmp->bc->astbuf);
+	
+	len = misdn_ibuf_usedcount(tmp->bc->astbuf);
 	
 	/*shrinken len if necessary, we transmit at maximum 4k*/
 	len = len<=sizeof(tmp->ast_rd_buf)?len:sizeof(tmp->ast_rd_buf);
 	
-	ibuf_memcpy_r(tmp->ast_rd_buf, tmp->bc->astbuf,len);
+	misdn_ibuf_memcpy_r(tmp->ast_rd_buf, tmp->bc->astbuf,len);
 	
 	tmp->frame.frametype  = AST_FRAME_VOICE;
 	tmp->frame.subclass = AST_FORMAT_ALAW;
@@ -1734,17 +1655,17 @@ int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
 	
 	
 	if (p->holded ) {
-		chan_misdn_log(5, p->bc->stack->port, "misdn_write: Returning because holded\n");
+		chan_misdn_log(5, p->bc->port, "misdn_write: Returning because holded\n");
 		return 0;
 	}
 	
 	if (p->notxtone) {
-		chan_misdn_log(5, p->bc->stack->port, "misdn_write: Returning because notxone\n");
+		chan_misdn_log(5, p->bc->port, "misdn_write: Returning because notxone\n");
 		return 0;
 	}
 	
 	if ( !(frame->subclass & prefformat)) {
-		chan_misdn_log(0, p->bc->stack->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
+		chan_misdn_log(0, p->bc->port, "Got Unsupported Frame with Format:%d\n", frame->subclass);
 	}
 	
 	
@@ -1766,9 +1687,12 @@ int misdn_write(struct ast_channel *ast, struct ast_frame *frame)
 }
 
 
+enum ast_bridge_result  misdn_bridge (struct ast_channel *c0,
+				      struct ast_channel *c1, int flags,
+				      struct ast_frame **fo,
+				      struct ast_channel **rc,
+				      int timeoutms)
 
-int misdn_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags,
-		  struct ast_frame **fo, struct ast_channel **rc)
 {
 	struct chan_list *ch1,*ch2;
 	struct ast_channel *carr[2], *who;
@@ -1791,30 +1715,26 @@ int misdn_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags,
 	misdn_cfg_get( 0, MISDN_GEN_BRIDGING, &bridging, sizeof(int));
 	if (bridging) {
 		int ecwb;
-		misdn_cfg_get( ch1->bc->stack->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
+		misdn_cfg_get( ch1->bc->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
 		if ( !ecwb ) {
-			chan_misdn_log(0, ch1->bc->stack->port, "Disabling Echo Cancellor when Bridged\n");
+			chan_misdn_log(0, ch1->bc->port, "Disabling Echo Cancellor when Bridged\n");
 			ch1->bc->ec_enable=0;
 			manager_ec_disable(ch1->bc);
 		}
-		misdn_cfg_get( ch2->bc->stack->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
+		misdn_cfg_get( ch2->bc->port, MISDN_CFG_ECHOCANCELWHENBRIDGED, &ecwb, sizeof(int));
 		if ( !ecwb ) {
-			chan_misdn_log(0, ch2->bc->stack->port, "Disabling Echo Cancellor when Bridged\n");
+			chan_misdn_log(0, ch2->bc->port, "Disabling Echo Cancellor when Bridged\n");
 			ch2->bc->ec_enable=0;
 			manager_ec_disable(ch2->bc);
 		}
 		
 		/* trying to make a mISDN_dsp conference */
-		chan_misdn_log(0, ch1->bc->stack->port, "I SEND: Making conference with Number:%d\n", (ch1->bc->pid<<1) +1);
-		manager_ph_control(ch1->bc, CMX_RECEIVE_OFF, 0);
-		manager_ph_control(ch2->bc, CMX_RECEIVE_OFF, 0);
-    
+		chan_misdn_log(0, ch1->bc->port, "I SEND: Making conference with Number:%d\n", (ch1->bc->pid<<1) +1);
 
-		manager_ph_control(ch1->bc, CMX_CONF_JOIN, (ch1->bc->pid<<1) +1);
-		manager_ph_control(ch2->bc, CMX_CONF_JOIN, (ch1->bc->pid<<1) +1);
+		misdn_lib_bridge(ch1->bc,ch2->bc);
 	}
-  
-	chan_misdn_log(1, ch1->bc->stack->port, "* Makeing Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad);
+	
+	chan_misdn_log(1, ch1->bc->port, "* Makeing Native Bridge between %s and %s\n", ch1->bc->oad, ch2->bc->oad);
   
 	while(1) {
 		to=-1;
@@ -1840,12 +1760,7 @@ int misdn_bridge (struct ast_channel *c0, struct ast_channel *c1, int flags,
 	}
   
 	if (bridging) {
-		manager_ph_control(ch1->bc, CMX_RECEIVE_ON, 0);
-		manager_ph_control(ch2->bc, CMX_RECEIVE_ON, 0);
-    
-		chan_misdn_log(0, ch1->bc->stack->port, "I SEND: Splitting conference with Number:%d\n", (ch1->bc->pid<<1) +1);
-		manager_ph_control(ch1->bc, CMX_CONF_SPLIT, (ch1->bc->pid<<1) +1);
-		manager_ph_control(ch2->bc, CMX_CONF_SPLIT, (ch1->bc->pid<<1) +1);
+		misdn_lib_split_bridge(ch1->bc,ch2->bc);
 	}
   
 	return 0;
@@ -1984,10 +1899,10 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
 						if ((l1 && port_up) || !l1)	{
 							newbc = misdn_lib_get_free_bc(port, robin_channel);
 							if (newbc) {
-								chan_misdn_log(4, port, " Success! Found port:%d channel:%d\n", newbc->stack->port, newbc->channel);
+								chan_misdn_log(4, port, " Success! Found port:%d channel:%d\n", newbc->port, newbc->channel);
 								if (port_up)
 									chan_misdn_log(4, port, "def_l1:%d, portup:%d\n", l1, port_up);
-								rr->port = newbc->stack->port;
+								rr->port = newbc->port;
 								rr->channel = newbc->channel;
 								break;
 							}
@@ -2037,8 +1952,7 @@ static struct ast_channel *misdn_request(const char *type, int format, void *dat
 	
 	cl->bc=newbc;
 	
-	tmp = misdn_new(cl, AST_STATE_RESERVED, buf, "",ext, NULL, format, port, channel);
-	
+	tmp = misdn_new(cl, AST_STATE_RESERVED, buf, "default", ext, ext, format, port, channel);
 	
 	return tmp;
 }
@@ -2086,7 +2000,7 @@ struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name,
 {
 	struct ast_channel *tmp;
 	
-	tmp = ast_channel_alloc(1);
+	tmp = ast_channel_alloc(0);
 	
 	if (tmp) {
 		chan_misdn_log(2, 0, " --> * NEW CHANNEL dad:%s oad:%s ctx:%s\n",exten,callerid, context);
@@ -2142,31 +2056,26 @@ struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name,
 			tmp->fds[0]=chlist->pipe[0];
 			
 		}
-		misdn_cfg_get( 0, MISDN_CFG_LANGUAGE, tmp->language, sizeof(tmp->language));
-
+		
 		if (chlist->bc) {
-
-			if (misdn_cfg_is_port_valid(chlist->bc->stack->port) ) {
-				{
-					char buf[256];
-					ast_group_t pg,cg;
-					
-					misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
-					misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
-					
-					chan_misdn_log(2, chlist->bc->stack->port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
-					tmp->pickupgroup=pg;
-					tmp->callgroup=cg;
-				}
-				misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_TXGAIN, &chlist->bc->txgain, sizeof(int));
-				misdn_cfg_get( chlist->bc->stack->port, MISDN_CFG_RXGAIN, &chlist->bc->rxgain, sizeof(int));
-				chan_misdn_log(2, chlist->bc->stack->port, " --> rxgain:%d txgain:%d\n",chlist->bc->rxgain,chlist->bc->txgain);
+			int port=chlist->bc->port;
+			misdn_cfg_get( port, MISDN_CFG_LANGUAGE, tmp->language, sizeof(tmp->language));
+			
+			{
+				char buf[256];
+				ast_group_t pg,cg;
 				
+				misdn_cfg_get(port, MISDN_CFG_PICKUPGROUP, &pg, sizeof(pg));
+				misdn_cfg_get(port, MISDN_CFG_CALLGROUP, &cg, sizeof(cg));
 				
-			} else {
-				chan_misdn_log(0, 0, " --> !! Config Not found in misdn_new port:%d\n",chlist->bc->stack->port);
+				chan_misdn_log(2, port, " --> * CallGrp:%s PickupGrp:%s\n",ast_print_group(buf,sizeof(buf),cg),ast_print_group(buf,sizeof(buf),pg));
+				tmp->pickupgroup=pg;
+				tmp->callgroup=cg;
 			}
-
+			misdn_cfg_get(port, MISDN_CFG_TXGAIN, &chlist->bc->txgain, sizeof(int));
+			misdn_cfg_get(port, MISDN_CFG_RXGAIN, &chlist->bc->rxgain, sizeof(int));
+			chan_misdn_log(2, port, " --> rxgain:%d txgain:%d\n",chlist->bc->rxgain,chlist->bc->txgain);
+			
 		} else {
 			chan_misdn_log(3, 0, " --> Not Setting Pickupgroup, we have no bc yet\n");
 		}
@@ -2176,8 +2085,10 @@ struct ast_channel *misdn_new(struct chan_list *chlist, int state, char * name,
 			tmp->rings = 1;
 		else
 			tmp->rings = 0;
-	} else
+	} else {
 		ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
+		chan_misdn_log(0,0,"Unable to allocate channel structure\n");
+	}
 	
 	return tmp;
 }
@@ -2189,10 +2100,19 @@ int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 	struct ast_frame frame;
 	
 	/* If in hold state we drop frame .. */
-	if (tmp->holded || tmp->state == MISDN_CLEANING ) return 0; 
+	if (tmp->holded ) return 0;
+
+	switch(tmp->state) {
+	case MISDN_CLEANING:
+	case MISDN_EXTCANTMATCH:
+	case MISDN_WAITING4DIGS:
+		return 0;
+	default:
+		break;
+	}
 	
 	if (tmp->norxtone) {
-		chan_misdn_log(3, tmp->bc->stack->port, "misdn_tx2ast_frm: Returning because norxtone\n");
+		chan_misdn_log(3, tmp->bc->port, "misdn_tx2ast_frm: Returning because norxtone\n");
 		return 0;
 	}
 	
@@ -2210,7 +2130,7 @@ int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 		if (tmp->trans)
 			f2=ast_translate(tmp->trans, &frame,0);
 		else {
-			chan_misdn_log(0, tmp->bc->stack->port, "No T-Path found\n");
+			chan_misdn_log(0, tmp->bc->port, "No T-Path found\n");
 			return 0;
 		}
 		
@@ -2251,7 +2171,7 @@ int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 				fr.mallocd =0 ;
 				fr.offset= 0 ;
 				
-				chan_misdn_log(2, tmp->bc->stack->port, " --> * SEND: DTMF (AST_DSP) :%c\n",f->subclass);
+				chan_misdn_log(2, tmp->bc->port, " --> * SEND: DTMF (AST_DSP) :%c\n",f->subclass);
 				ast_queue_frame(tmp->ast, &fr);
 				
 				frame.frametype = AST_FRAME_NULL;
@@ -2270,7 +2190,7 @@ int misdn_tx2ast_frm(struct chan_list * tmp, char * buf,  int len )
 		for (i=0; i<  max ; i++) printf("%2.2x ",((char*) frame.data)[i]);
 		printf ("\n");
 #endif
-		chan_misdn_log(9, tmp->bc->stack->port, "Queueing %d bytes 2 Asterisk\n",len);
+		chan_misdn_log(9, tmp->bc->port, "Queueing %d bytes 2 Asterisk\n",len);
 		
 		ast_queue_frame(tmp->ast,&frame);
 		
@@ -2290,7 +2210,7 @@ struct chan_list *find_chan_by_l3id(struct chan_list *list, unsigned long l3id)
 		if (help->l3id == l3id ) return help;
 	}
   
-	chan_misdn_log(4, list? (list->bc? list->bc->stack->port : 0) : 0, "$$$ find_chan: No channel found with l3id:%x\n",l3id);
+	chan_misdn_log(4, list? (list->bc? list->bc->port : 0) : 0, "$$$ find_chan: No channel found with l3id:%x\n",l3id);
   
 	return NULL;
 }
@@ -2302,7 +2222,7 @@ struct chan_list *find_chan_by_bc(struct chan_list *list, struct misdn_bchannel
 		if (help->bc == bc) return help;
 	}
   
-	chan_misdn_log(4, bc->stack->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
+	chan_misdn_log(4, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
   
 	return NULL;
 }
@@ -2312,23 +2232,23 @@ struct chan_list *find_holded(struct chan_list *list, struct misdn_bchannel *bc)
 {
 	struct chan_list *help=list;
 	
-	chan_misdn_log(4, bc->stack->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n",bc->channel, bc->oad,bc->dad);
+	chan_misdn_log(4, bc->port, "$$$ find_holded: channel:%d oad:%s dad:%s\n",bc->channel, bc->oad,bc->dad);
 	for (;help; help=help->next) {
-		chan_misdn_log(4, bc->stack->port, "$$$ find_holded: --> holded:%d channel:%d\n",help->bc->holded, help->bc->channel);
-		if (help->bc->stack == bc->stack
+		chan_misdn_log(4, bc->port, "$$$ find_holded: --> holded:%d channel:%d\n",help->bc->holded, help->bc->channel);
+		if (help->bc->port == bc->port
 		    && help->bc->holded ) return help;
 	}
 	
-	chan_misdn_log(4, bc->stack->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
+	chan_misdn_log(4, bc->port, "$$$ find_chan: No channel found for oad:%s dad:%s\n",bc->oad,bc->dad);
   
 	return NULL;
 }
 
 void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
 {
-	chan_misdn_log(4, chan->bc? chan->bc->stack->port : 0, "* Queuing chan %p\n",chan);
+	chan_misdn_log(4, chan->bc? chan->bc->port : 0, "* Queuing chan %p\n",chan);
   
-	pthread_mutex_lock(&cl_te_lock);
+	ast_mutex_lock(&cl_te_lock);
 	if (!*list) {
 		*list = chan;
 	} else {
@@ -2337,7 +2257,7 @@ void cl_queue_chan(struct chan_list **list, struct chan_list *chan)
 		help->next=chan;
 	}
 	chan->next=NULL;
-	pthread_mutex_unlock(&cl_te_lock);
+	ast_mutex_unlock(&cl_te_lock);
 }
 
 void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan) 
@@ -2347,15 +2267,16 @@ void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan)
 	if (chan->trans)
 		ast_translator_free_path(chan->trans);
 	
-	pthread_mutex_lock(&cl_te_lock);
+
+	ast_mutex_lock(&cl_te_lock);
 	if (!*list) {
-		pthread_mutex_unlock(&cl_te_lock);
+		ast_mutex_unlock(&cl_te_lock);
 		return;
 	}
   
 	if (*list == chan) {
 		*list=(*list)->next;
-		pthread_mutex_unlock(&cl_te_lock);
+		ast_mutex_unlock(&cl_te_lock);
 		return ;
 	}
   
@@ -2364,13 +2285,13 @@ void cl_dequeue_chan(struct chan_list **list, struct chan_list *chan)
 		for (;help->next; help=help->next) {
 			if (help->next == chan) {
 				help->next=help->next->next;
-				pthread_mutex_unlock(&cl_te_lock);
+				ast_mutex_unlock(&cl_te_lock);
 				return;
 			}
 		}
 	}
 	
-	pthread_mutex_unlock(&cl_te_lock);
+	ast_mutex_unlock(&cl_te_lock);
 }
 
 /** Channel Queue End **/
@@ -2391,7 +2312,7 @@ static void release_chan(struct misdn_bchannel *bc) {
 		} 
 		release_unlock;
 		
-		chan_misdn_log(1, bc->stack->port, "Trying to Release bc with l3id: %x\n",bc->l3_id);
+		chan_misdn_log(1, bc->port, "Trying to Release bc with l3id: %x\n",bc->l3_id);
 		if (ch) {
 			if (ast)
 				chan_misdn_trace_call(ast,1,"I->*: EVENT_RELEASE\n");
@@ -2400,8 +2321,8 @@ static void release_chan(struct misdn_bchannel *bc) {
 			close(ch->pipe[1]);
 			
 			if (ast && MISDN_ASTERISK_PVT(ast)) {
-				chan_misdn_log(1, bc->stack->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n",bc?bc->pid:-1, ast->context, ast->exten,AST_CID_P(ast),misdn_get_ch_state(ch));
-				chan_misdn_log(3, bc->stack->port, " --> * State Down\n");
+				chan_misdn_log(1, bc->port, "* RELEASING CHANNEL pid:%d ctx:%s dad:%s oad:%s state: %s\n",bc?bc->pid:-1, ast->context, ast->exten,AST_CID_P(ast),misdn_get_ch_state(ch));
+				chan_misdn_log(3, bc->port, " --> * State Down\n");
 				/* copy cause */
 				send_cause2ast(ast,bc);
 				
@@ -2409,7 +2330,7 @@ static void release_chan(struct misdn_bchannel *bc) {
 				
       
 				if (ast->_state != AST_STATE_RESERVED) {
-					chan_misdn_log(3, bc->stack->port, " --> Setting AST State to down\n");
+					chan_misdn_log(3, bc->port, " --> Setting AST State to down\n");
 					ast_setstate(ast, AST_STATE_DOWN);
 				}
 				
@@ -2417,37 +2338,50 @@ static void release_chan(struct misdn_bchannel *bc) {
 				case MISDN_EXTCANTMATCH:
 				case MISDN_WAITING4DIGS:
 				{
-					chan_misdn_log(3,  bc->stack->port, " --> * State Wait4dig | ExtCantMatch\n");
+					chan_misdn_log(3,  bc->port, " --> * State Wait4dig | ExtCantMatch\n");
 					ast_hangup(ast);
 				}
 				break;
-	
-				case MISDN_CALLING:
-				case MISDN_CALLING_ACKNOWLEDGE:
+				
 				case MISDN_DIALING:
+				case MISDN_CALLING_ACKNOWLEDGE:
 				case MISDN_PROGRESS:
-					chan_misdn_log(2,  bc->stack->port, "* --> In State Calling|Dialing\n");
-					chan_misdn_log(2,  bc->stack->port, "* --> Queue Hangup\n");
-	
+					chan_misdn_log(2,  bc->port, "* --> In State Dialin\n");
+					chan_misdn_log(2,  bc->port, "* --> Queue Hangup\n");
+					
 
-					ast_queue_control(ast, AST_CONTROL_HANGUP);
+					ast_queue_hangup(ast);
 					break;
+				case MISDN_CALLING:
+					
+					chan_misdn_log(2,  bc->port, "* --> In State Callin\n");
+					
+					if (!bc->nt) {
+						chan_misdn_log(2,  bc->port, "* --> Queue Hangup\n");
+						ast_queue_hangup(ast);
+					} else {
+						chan_misdn_log(2,  bc->port, "* --> Hangup\n");
+						ast_queue_hangup(ast);
+						//ast_hangup(ast);
+					}
+					break;
+					
 				case MISDN_CLEANING:
 					/* this state comes out of ast so we mustnt call a ast function ! */
-					chan_misdn_log(2,  bc->stack->port, "* --> In StateCleaning\n");
+					chan_misdn_log(2,  bc->port, "* --> In StateCleaning\n");
 					break;
 				case MISDN_HOLD_DISCONNECT:
-					chan_misdn_log(2,  bc->stack->port, "* --> In HOLD_DISC\n");
+					chan_misdn_log(2,  bc->port, "* --> In HOLD_DISC\n");
 					break;
 				default:
-					chan_misdn_log(2,  bc->stack->port, "* --> In State Default\n");
-					chan_misdn_log(2,  bc->stack->port, "* --> Queue Hangup\n");
+					chan_misdn_log(2,  bc->port, "* --> In State Default\n");
+					chan_misdn_log(2,  bc->port, "* --> Queue Hangup\n");
 	
 					
 					if (ast && MISDN_ASTERISK_PVT(ast)) {
 						ast_queue_hangup(ast);
 					} else {
-						chan_misdn_log (0,  bc->stack->port, "!! Not really queued!\n");
+						chan_misdn_log (0,  bc->port, "!! Not really queued!\n");
 					}
 				}
 			}
@@ -2488,12 +2422,12 @@ void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct
   
 	ch->state=MISDN_DIALING;
 	
-	if (bc->stack->mode == NT_MODE) {
+	if (bc->nt) {
 		int ret; 
 		ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
 	} else {
 		int ret;
-		if (bc->stack->ptp) {
+		if ( misdn_lib_is_ptp(bc->port)) {
 			ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
 		} else {
 			ret = misdn_lib_send_event(bc, EVENT_PROCEEDING );
@@ -2502,14 +2436,14 @@ void do_immediate_setup(struct misdn_bchannel *bc,struct chan_list *ch , struct
 
 	manager_send_tone(bc,TONE_DIAL);  
   
-	chan_misdn_log(1, bc->stack->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, AST_CID_P(ast));
+	chan_misdn_log(1, bc->port, "* Starting Ast ctx:%s dad:%s oad:%s with 's' extension\n", ast->context, ast->exten, AST_CID_P(ast));
   
 	strncpy(ast->exten,"s", 2);
   
 	if (ast_pbx_start(ast)<0) {
 		ast=NULL;
 		manager_send_tone(bc,TONE_BUSY);
-		if (bc->stack->mode == NT_MODE)
+		if (bc->nt)
 			misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
 		else
 			misdn_lib_send_event(bc, EVENT_DISCONNECT );
@@ -2547,14 +2481,14 @@ void send_cause2ast(struct ast_channel *ast, struct misdn_bchannel*bc) {
  	case 4:
  	case 22:
  	case 27:
-		chan_misdn_log(1, bc?bc->stack->port:0, " --> * SEND: Queue Congestion pid:%d\n", bc?bc->pid:-1);
+		chan_misdn_log(1, bc?bc->port:0, " --> * SEND: Queue Congestion pid:%d\n", bc?bc->pid:-1);
 		
 		ast_queue_control(ast, AST_CONTROL_CONGESTION);
 		break;
 		
 	case 21:
 	case 17: /* user busy */
-		chan_misdn_log(1,  bc?bc->stack->port:0, " --> * SEND: Queue Busy pid:%d\n", bc?bc->pid:-1);
+		chan_misdn_log(1,  bc?bc->port:0, " --> * SEND: Queue Busy pid:%d\n", bc?bc->pid:-1);
 		
 		ast_queue_control(ast, AST_CONTROL_BUSY);
 		
@@ -2574,14 +2508,14 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		ch=find_chan_by_l3id(cl_te, bc->l3_id);
 	
 	if (event != EVENT_BCHAN_DATA) { /*  Debug Only Non-Bchan */
-		chan_misdn_log(1, bc->stack->port, "I IND :%s oad:%s dad:%s port:%d\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->stack->port);
+		chan_misdn_log(1, bc->port, "I IND :%s oad:%s dad:%s port:%d\n", manager_isdn_get_info(event), bc->oad, bc->dad, bc->port);
 		misdn_lib_log_ies(bc);
 	}
 	
 	if (event != EVENT_SETUP) {
 		if (!ch) {
 			if (event != EVENT_CLEANUP )
-				ast_log(LOG_WARNING, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n",bc->l3_id, bc, manager_isdn_get_info( event), bc->stack->port,bc->channel);
+				ast_log(LOG_WARNING, "Chan not existing at the moment bc->l3id:%x bc:%p event:%s port:%d channel:%d\n",bc->l3_id, bc, manager_isdn_get_info( event), bc->port,bc->channel);
 			return -1;
 		}
 	}
@@ -2626,7 +2560,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		fr.mallocd =0 ;
 		fr.offset= 0 ;
 		
-		chan_misdn_log(2, bc->stack->port, " --> DTMF:%c\n", bc->dtmf);
+		chan_misdn_log(2, bc->port, " --> DTMF:%c\n", bc->dtmf);
 		
 		ast_queue_frame(ch->ast, &fr);
 	}
@@ -2656,16 +2590,16 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 				strncpy(ch->ast->exten, bc->dad, l);
 				ch->ast->exten[l-1] = 0;
 			}
-/*			chan_misdn_log(5, bc->stack->port, "Can Match Extension: dad:%s oad:%s\n",bc->dad,bc->oad);*/
+/*			chan_misdn_log(5, bc->port, "Can Match Extension: dad:%s oad:%s\n",bc->dad,bc->oad);*/
 			
 			char bc_context[BUFFERSIZE];
-			misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
+			misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
 			if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
-				chan_misdn_log(0, bc->stack->port, "Extension can never match, so disconnecting\n");
+				chan_misdn_log(0, bc->port, "Extension can never match, so disconnecting\n");
 				manager_send_tone(bc,TONE_BUSY);
 				ch->state=MISDN_EXTCANTMATCH;
 				bc->out_cause=1;
-				if (bc->stack->mode == NT_MODE)
+				if (bc->nt)
 					misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
 				else
 					misdn_lib_send_event(bc, EVENT_DISCONNECT );
@@ -2675,11 +2609,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 				ch->state=MISDN_DIALING;
 	  
 				manager_send_tone(bc,TONE_NONE);
-/*				chan_misdn_log(1, bc->stack->port, " --> * Starting Ast ctx:%s\n", ch->ast->context);*/
+/*				chan_misdn_log(1, bc->port, " --> * Starting Ast ctx:%s\n", ch->ast->context);*/
 				if (ast_pbx_start(ch->ast)<0) {
-					chan_misdn_log(0, bc->stack->port, "ast_pbx_start returned < 0 in INFO\n");
+					chan_misdn_log(0, bc->port, "ast_pbx_start returned < 0 in INFO\n");
 					manager_send_tone(bc,TONE_BUSY);
-					if (bc->stack->mode == NT_MODE)
+					if (bc->nt)
 						misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
 					else
 						misdn_lib_send_event(bc, EVENT_DISCONNECT );
@@ -2725,14 +2659,14 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 	{
 		struct chan_list *ch=find_chan_by_bc(cl_te, bc);
 		if (ch && ch->state != MISDN_NOTHING ) {
-			chan_misdn_log(1, bc->stack->port, " --> Ignoring Call we have already one\n");
+			chan_misdn_log(1, bc->port, " --> Ignoring Call we have already one\n");
 			return RESPONSE_IGNORE_SETUP_WITHOUT_CLOSE; /*  Ignore MSNs which are not in our List */
 		}
 	}
 	
-	int msn_valid = misdn_cfg_is_msn_valid(bc->stack->port, bc->dad);
-	if (bc->stack->mode == TE_MODE && ! msn_valid) {
-		chan_misdn_log(1, bc->stack->port, " --> Ignoring Call, its not in our MSN List\n");
+	int msn_valid = misdn_cfg_is_msn_valid(bc->port, bc->dad);
+	if (!bc->nt && ! msn_valid) {
+		chan_misdn_log(1, bc->port, " --> Ignoring Call, its not in our MSN List\n");
 		return RESPONSE_IGNORE_SETUP; /*  Ignore MSNs which are not in our List */
 	}
 	
@@ -2742,7 +2676,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		struct chan_list *ch=init_chan_list();
 		struct ast_channel *chan;
 		char name[128];
-		if (!ch) { chan_misdn_log(0, bc->stack->port, "cb_events: malloc for chan_list failed!\n"); return 0;}
+		if (!ch) { chan_misdn_log(0, bc->port, "cb_events: malloc for chan_list failed!\n"); return 0;}
 		
 		ch->bc = bc;
 		ch->l3id=bc->l3_id;
@@ -2754,11 +2688,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			char prefix[BUFFERSIZE]="";
 			switch( bc->onumplan ) {
 			case NUMPLAN_INTERNATIONAL:
-				misdn_cfg_get( bc->stack->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
+				misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
 				break;
 	  
 			case NUMPLAN_NATIONAL:
-				misdn_cfg_get( bc->stack->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
+				misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
 				break;
 	  
 	  
@@ -2781,9 +2715,9 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			}
 			
 			if (!ast_strlen_zero(bc->oad))
-				sprintf(name,"mISDN/%d/%s",bc->stack->port,bc->oad);
+				sprintf(name,"mISDN/%d/%s",bc->port,bc->oad);
 			else
-				sprintf(name,"mISDN/%d",bc->stack->port);
+				sprintf(name,"mISDN/%d",bc->port);
 
 
 			if (!ast_strlen_zero(bc->dad)) {
@@ -2799,11 +2733,11 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			
 			switch( bc->dnumplan ) {
 			case NUMPLAN_INTERNATIONAL:
-				misdn_cfg_get( bc->stack->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
+				misdn_cfg_get( bc->port, MISDN_CFG_INTERNATPREFIX, prefix, BUFFERSIZE);
 				break;
 				
 			case NUMPLAN_NATIONAL:
-				misdn_cfg_get( bc->stack->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
+				misdn_cfg_get( bc->port, MISDN_CFG_NATPREFIX, prefix, BUFFERSIZE);
 				break;
 				
 				
@@ -2826,14 +2760,20 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			}
 			
 			char bc_context[BUFFERSIZE];
-			misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
-			chan=misdn_new(ch, AST_STATE_RING,name ,bc_context, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->stack->port, bc->channel);
+			misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
+			chan=misdn_new(ch, AST_STATE_RING,name ,bc_context, bc->dad, bc->oad, AST_FORMAT_ALAW, bc->port, bc->channel);
+			
+			if (!chan) {
+				misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
+				return 0;
+			}
 			
+			ch->ast = chan;
 			pbx_builtin_setvar_helper(ch->ast,"REDIRECTING_NUMBER",bc->rad);
 			
 		}
 
-		ch->ast = chan;
+		
 
 		chan_misdn_trace_call(chan,1,"I->*: EVENT_SETUP\n");
 		
@@ -2881,16 +2821,16 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		*/
 		{
 			
-			misdn_cfg_get( bc->stack->port, MISDN_CFG_LANGUAGE, chan->language, sizeof(chan->language));
+			misdn_cfg_get( bc->port, MISDN_CFG_LANGUAGE, chan->language, sizeof(chan->language));
 			int ai;
-			misdn_cfg_get( bc->stack->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
+			misdn_cfg_get( bc->port, MISDN_CFG_ALWAYS_IMMEDIATE, &ai, sizeof(ai));
 			if ( ai ) {
 				do_immediate_setup(bc, ch , chan);
 				break;
 			}
 
 			int immediate;
-			misdn_cfg_get( bc->stack->port, MISDN_CFG_IMMEDIATE, &immediate, sizeof(int));
+			misdn_cfg_get( bc->port, MISDN_CFG_IMMEDIATE, &immediate, sizeof(int));
 			
 			if (ast_strlen_zero(bc->orig_dad) && immediate ) {
 				do_immediate_setup(bc, ch , chan);
@@ -2903,13 +2843,13 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		    we'll jump into the dialplan **/
 		
 		char bc_context[BUFFERSIZE];
-		misdn_cfg_get( bc->stack->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
+		misdn_cfg_get( bc->port, MISDN_CFG_CONTEXT, bc_context, BUFFERSIZE);
 		if(!ast_canmatch_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
-			chan_misdn_log(0, bc->stack->port, "Extension can never match, so disconnecting\n");
+			chan_misdn_log(0, bc->port, "Extension can never match, so disconnecting\n");
 			manager_send_tone(bc,TONE_BUSY);
 			ch->state=MISDN_EXTCANTMATCH;
 			bc->out_cause=1;
-			if (bc->stack->mode == NT_MODE)
+			if (bc->nt)
 				misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
 			else
 				misdn_lib_send_event(bc, EVENT_DISCONNECT );
@@ -2919,7 +2859,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		if (ast_exists_extension(ch->ast, bc_context, bc->dad, 1, bc->oad)) {
 			ch->state=MISDN_DIALING;
 	
-			if (bc->stack->mode == NT_MODE) {
+			if (bc->nt) {
 				int ret; 
 				ret = misdn_lib_send_event(bc, EVENT_SETUP_ACKNOWLEDGE );
 			} else {
@@ -2928,10 +2868,10 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			}
 	
 			if (ast_pbx_start(chan)<0) {
-				chan_misdn_log(0, bc->stack->port, "ast_pbx_start returned <0 in SETUP\n");
+				chan_misdn_log(0, bc->port, "ast_pbx_start returned <0 in SETUP\n");
 				chan=NULL;
 				manager_send_tone(bc,TONE_BUSY);
-				if (bc->stack->mode == NT_MODE)
+				if (bc->nt)
 					misdn_lib_send_event(bc, EVENT_RELEASE_COMPLETE );
 				else
 					misdn_lib_send_event(bc, EVENT_DISCONNECT );
@@ -2993,7 +2933,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 	}
 	break;
 	case EVENT_PROGRESS:
-		if (bc->stack->mode == TE_MODE ) {
+		if (!bc->nt ) {
 			if ( misdn_cap_is_speech(bc->capability) &&
 			     misdn_inband_avail(bc)
 				) {
@@ -3102,12 +3042,12 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		} else {
 			int len=bc->bframe_len;
 			
-			if (bc->bframe_len > ibuf_freecount(bc->astbuf)) {
+			if (bc->bframe_len > misdn_ibuf_freecount(bc->astbuf)) {
 				ast_log(LOG_DEBUG, "sbuf overflow!\n");
-				len=ibuf_freecount(bc->astbuf);
+				len=misdn_ibuf_freecount(bc->astbuf);
 			}
 			
-			ibuf_memcpy_w(bc->astbuf, bc->bframe, len);
+			misdn_ibuf_memcpy_w(bc->astbuf, bc->bframe, len);
 			
 			{
 				char blah[1]="\0";
@@ -3136,7 +3076,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 		{
 			switch (ch->state) {
 			case MISDN_CALLING:
-				chan_misdn_log(0, bc?bc->stack->port:0, "GOT TIMOUT AT CALING pid:%d\n", bc?bc->pid:-1);
+				chan_misdn_log(0, bc?bc->port:0, "GOT TIMOUT AT CALING pid:%d\n", bc?bc->pid:-1);
 					break;
 			case MISDN_DIALING:
 			case MISDN_PROGRESS:
@@ -3177,10 +3117,10 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 	case EVENT_HOLD:
 	{
 		int hold_allowed;
-		misdn_cfg_get( bc->stack->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int));
+		misdn_cfg_get( bc->port, MISDN_CFG_HOLD_ALLOWED, &hold_allowed, sizeof(int));
 		
 		if (!hold_allowed) {
-			chan_misdn_log(0, bc->stack->port, "Hold not allowed on port:%d\n", bc->stack->port);
+			chan_misdn_log(0, bc->port, "Hold not allowed on port:%d\n", bc->port);
 			misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
 			break;
 		}
@@ -3189,7 +3129,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			struct chan_list *holded_ch=find_holded(cl_te, bc);
 			if (holded_ch) {
 				misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
-				chan_misdn_log(0, bc->stack->port, "We can't use RETRIEVE at the moment due to mISDN bug!\n");
+				chan_misdn_log(0, bc->port, "We can't use RETRIEVE at the moment due to mISDN bug!\n");
 				break;
 			}
 		}
@@ -3202,7 +3142,7 @@ cb_events(enum event_e event, struct misdn_bchannel *bc, void *user_data)
 			misdn_lib_send_event(bc, EVENT_HOLD_ACKNOWLEDGE);
 		} else {
 			misdn_lib_send_event(bc, EVENT_HOLD_REJECT);
-			chan_misdn_log(0, bc->stack->port, "We aren't bridged to anybody\n");
+			chan_misdn_log(0, bc->port, "We aren't bridged to anybody\n");
 		}
 	} 
 	break;
@@ -3263,8 +3203,8 @@ int load_module(void)
 			tracing = 1;
 	}
 
-	pthread_mutex_init(&cl_te_lock, NULL);
-	pthread_mutex_init(&release_lock_mutex, NULL);
+	ast_mutex_init(&cl_te_lock);
+	ast_mutex_init(&release_lock_mutex);
 
 	misdn_cfg_get_ports_string(ports);
 	if (strlen(ports))
@@ -3300,8 +3240,7 @@ int load_module(void)
 	ast_cli_register(&cli_show_config);
 	ast_cli_register(&cli_show_port);
 	ast_cli_register(&cli_show_stacks);
-	ast_cli_register(&cli_show_fullstacks);
-	ast_cli_register(&cli_flush_stack);
+
 	ast_cli_register(&cli_restart_port);
 	ast_cli_register(&cli_port_up);
 	ast_cli_register(&cli_set_debug);
@@ -3353,8 +3292,6 @@ int unload_module(void)
 	ast_cli_unregister(&cli_show_config);
 	ast_cli_unregister(&cli_show_port);
 	ast_cli_unregister(&cli_show_stacks);
-	ast_cli_unregister(&cli_show_fullstacks);
-	ast_cli_unregister(&cli_flush_stack);
 	ast_cli_unregister(&cli_restart_port);
 	ast_cli_unregister(&cli_port_up);
 	ast_cli_unregister(&cli_set_debug);
@@ -3570,16 +3507,16 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 			
 		case 'd' :
 			strncpy(ch->bc->display,++tok,84);
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Display:%s\n",ch->bc->display);
+			chan_misdn_log(1, ch->bc->port, "SETOPT: Display:%s\n",ch->bc->display);
 			break;
 			
 		case 'n':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: No DSP\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: No DSP\n");
 			ch->bc->nodsp=1;
 			break;
 
 		case 'j':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: No jitter\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: No jitter\n");
 			ch->bc->nojitter=1;
 			break;
       
@@ -3592,14 +3529,14 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 				if (rxgain<-8) rxgain=-8;
 				if (rxgain>8) rxgain=8;
 				ch->bc->rxgain=rxgain;
-				chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Volume:%d\n",rxgain);
+				chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n",rxgain);
 				break;
 			case 't':
 				txgain=atoi(++tok);
 				if (txgain<-8) txgain=-8;
 				if (txgain>8) txgain=8;
 				ch->bc->txgain=txgain;
-				chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Volume:%d\n",txgain);
+				chan_misdn_log(1, ch->bc->port, "SETOPT: Volume:%d\n",txgain);
 				break;
 			}
 			break;
@@ -3617,11 +3554,11 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 				strncpy(ch->bc->crypt_key, misdn_key_vector[keyidx], l);
 				ch->bc->crypt_key[l-1] = 0;
 			}
-			chan_misdn_log(0, ch->bc->stack->port, "SETOPT: crypt with key:%s\n",misdn_key_vector[keyidx]);
+			chan_misdn_log(0, ch->bc->port, "SETOPT: crypt with key:%s\n",misdn_key_vector[keyidx]);
 			break;
 
 		case 'e':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: EchoCancel\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: EchoCancel\n");
 			
 			if (neglect) {
 				ch->bc->ec_enable=0;
@@ -3637,9 +3574,9 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 			break;
       
 		case 'h':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Digital\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: Digital\n");
 			if (strlen(tok) > 1 && tok[1]=='1') {
-				chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Digital TRANS_DIGITAL\n");
+				chan_misdn_log(1, ch->bc->port, "SETOPT: Digital TRANS_DIGITAL\n");
 				ch->bc->nohdlc=1;
 				ch->bc->capability=INFO_CAPABILITY_DIGITAL_UNRESTRICTED;
 			} else {
@@ -3649,22 +3586,22 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 			break;
             
 		case 's':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Send DTMF\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: Send DTMF\n");
 			ch->bc->send_dtmf=1;
 			break;
 			
 		case 'f':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: Faxdetect\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: Faxdetect\n");
 			ch->faxdetect=1;
 			break;
 
 		case 'a':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: AST_DSP (for DTMF)\n");
+			chan_misdn_log(1, ch->bc->port, "SETOPT: AST_DSP (for DTMF)\n");
 			ch->ast_dsp=1;
 			break;
 
 		case 'p':
-			chan_misdn_log(1, ch->bc->stack->port, "SETOPT: callerpres: %s\n",&tok[1]);
+			chan_misdn_log(1, ch->bc->port, "SETOPT: callerpres: %s\n",&tok[1]);
 			/* CRICH: callingpres!!! */
 			if (strstr(tok,"allowed") ) {
 				ch->bc->pres=0;
@@ -3689,7 +3626,7 @@ static int misdn_set_opt_exec(struct ast_channel *chan, void *data)
 	}
 
 	if (ch->ast_dsp) {
-		chan_misdn_log(1,ch->bc->stack->port,"SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
+		chan_misdn_log(1,ch->bc->port,"SETOPT: with AST_DSP we deactivate mISDN_dsp\n");
 		ch->bc->nodsp=1;
 		ch->bc->nojitter=1;
 	}
diff --git a/channels/misdn/ie.c b/channels/misdn/ie.c
index b82d31b03c4558a102ea2b113f4e933b257f18aa..911ebaff286f6d74fc790ae3507856473b3433a5 100755
--- a/channels/misdn/ie.c
+++ b/channels/misdn/ie.c
@@ -20,7 +20,17 @@
   if qi is not NULL (TE-mode), offset is set
 */
 
-#include "isdn_lib.h"
+
+#include <string.h>
+
+
+#include "isdn_lib_intern.h"
+
+#include <mISDNlib.h>
+#include <isdn_net.h>
+#include <l3dss1.h>
+#include <net_l3.h>
+
 
 #define CENTREX_FAC     0x88
 #define CENTREX_ID      0xa1
@@ -667,8 +677,9 @@ void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int ch
 	unsigned char *p;
 	Q931_info_t *qi = (Q931_info_t *)(msg->data + mISDN_HEADER_LEN);
 	int l;
-	int pri = bc->stack->pri;
-
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	int pri = stack->pri;
+	
 	if (exclusive<0 || exclusive>1)
 	{
 		printf("%s: ERROR: exclusive(%d) is out of range.\n", __FUNCTION__, exclusive);
@@ -738,7 +749,8 @@ void enc_ie_channel_id(unsigned char **ntmode, msg_t *msg, int exclusive, int ch
 
 void dec_ie_channel_id(unsigned char *p, Q931_info_t *qi, int *exclusive, int *channel, int nt, struct misdn_bchannel *bc)
 {
-	int pri = bc->stack->pri;
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	int pri =stack->pri;
 
 	*exclusive = -1;
 	*channel = -1;
@@ -1332,7 +1344,8 @@ void enc_ie_facility(unsigned char **ntmode, msg_t *msg, unsigned char *facility
 void dec_ie_facility(unsigned char *p, Q931_info_t *qi, unsigned char *facility, int *facility_len, int nt, struct misdn_bchannel *bc)
 {
 	int i;
-
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	*facility_len = 0;
 
 	if (!nt)
@@ -1350,10 +1363,10 @@ void dec_ie_facility(unsigned char *p, Q931_info_t *qi, unsigned char *facility,
 	i = 0;
 	while(i < *facility_len)
 	{
-		cb_log(3, bc->stack->port, " %02x", facility[i]);
+		cb_log(3, stack->port, " %02x", facility[i]);
 		i++;
 	}
-	cb_log(3, bc->stack->port, "    facility\n");
+	cb_log(3, stack->port, "    facility\n");
 }
 
 
diff --git a/channels/misdn/isdn_lib.c b/channels/misdn/isdn_lib.c
index 3ef71e21834924abddfc9d46a1888e9af28d00eb..6c508e2cf656dc62b590943c5dd83026022b2041 100755
--- a/channels/misdn/isdn_lib.c
+++ b/channels/misdn/isdn_lib.c
@@ -11,6 +11,75 @@
  * the GNU General Public License
  */
 
+#include "isdn_lib_intern.h"
+
+
+int misdn_ibuf_freecount(void *buf)
+{
+	return ibuf_usedcount( (ibuffer_t*)buf);
+}
+
+int misdn_ibuf_usedcount(void *buf)
+{
+	return ibuf_usedcount( (ibuffer_t*)buf);
+}
+
+void misdn_ibuf_memcpy_r(char *to, void *buf, int len)
+{
+	ibuf_memcpy_r( to, (ibuffer_t*)buf, len);
+}
+
+void misdn_ibuf_memcpy_w(void *buf, char *from,  int len)
+{
+	ibuf_memcpy_w((ibuffer_t*)buf, from, len);
+}
+
+struct misdn_stack* get_misdn_stack( void );
+
+
+int misdn_lib_is_ptp(int port)
+{
+	struct misdn_stack *stack=get_misdn_stack();
+	for ( ; stack; stack=stack->next) {
+		if (stack->port == port) return stack->ptp;
+	}
+	return -1;
+}
+
+
+struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc)
+{
+	struct misdn_stack *stack=get_misdn_stack();
+	
+	
+	for ( ; stack; stack=stack->next) {
+		int i;
+		for (i=0; i <stack->b_num; i++) {
+			if ( bc->port == stack->port) return stack;
+		}
+	}
+
+	return NULL;
+}
+
+
+void get_show_stack_details(int port, char *buf)
+{
+	struct misdn_stack *stack=get_misdn_stack();
+	
+	for ( ; stack; stack=stack->next) {
+		if (stack->port == port) break;
+	}
+	
+	if (stack) {
+		sprintf(buf, "* Stack Addr: Port %d Type %s Prot. %s L2Link %s L1Link:%s", stack->upper_id & IF_CONTRMASK, stack->mode==NT_MODE?"NT":"TE", stack->ptp?"PTP":"PMP", stack->l2link?"UP":"DOWN", stack->l1link?"UP":"DOWN");
+	} else {
+		buf[0]=0;
+	}
+	
+}
+
+
 static int nt_err_cnt =0 ;
 
 enum global_states {
@@ -244,8 +313,9 @@ int send_msg (int midev, struct misdn_bchannel *bc, msg_t *dmsg)
 {
 	iframe_t *frm;
 	frm = (iframe_t *)dmsg->data;
-
-	frm->addr = (bc->stack->upper_id &  IF_ADDRMASK) | IF_DOWN ;
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
+	frm->addr = (stack->upper_id &  IF_ADDRMASK) | IF_DOWN ;
 	frm->dinfo = bc->l3_id;
   
 	frm->len = (dmsg->len) - mISDN_HEADER_LEN;
@@ -396,6 +466,8 @@ void empty_bc(struct misdn_bchannel *bc)
 	
 	bc->facility=FACILITY_NONE;
 	bc->facility_calldeflect_nr[0]=0;
+
+	bc->te_choose_channel = 0;
 }
 
 
@@ -403,21 +475,25 @@ int clean_up_bc(struct misdn_bchannel *bc)
 {
 	int ret=0;
 	unsigned char buff[32];
-	if (!bc || !bc->stack) return -1;
-  
+	struct misdn_stack * stack;
+	
+	if (!bc  ) return -1;
+	stack=get_stack_by_bc(bc);
+	if (!stack) return -1;
+	
 	if (!bc->upset) {
-		cb_log(5, bc->stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
+		cb_log(5, stack->port, "$$$ Already cleaned up bc with stid :%x\n", bc->b_stid);
 		return -1;
 	}
   
-	cb_log(5, bc->stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
-  
+	cb_log(5, stack->port, "$$$ Cleaning up bc with stid :%x\n", bc->b_stid);
+	
 	
 	if ( misdn_cap_is_speech(bc->capability) && bc->ec_enable) {
 		manager_ec_disable(bc);
 	}
 	
-	mISDN_write_frame(bc->stack->midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
+	mISDN_write_frame(stack->midev, buff, bc->layer_id, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
 	
 	
 	bc->b_stid = 0;
@@ -469,11 +545,67 @@ static int newteid=0;
 #define MAXPROCS 0x10
 #endif
 
+
+int misdn_lib_get_l1_up(struct misdn_stack *stack)
+{
+	/* Pull Up L1 if we have JOLLY */ 
+	iframe_t act;
+	act.prim = PH_ACTIVATE | REQUEST; 
+	act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+	act.dinfo = 0;
+	act.len = 0;
+
+	return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+
+}
+
+int misdn_lib_get_l2_up(struct misdn_stack *stack)
+{
+	
+	if (stack->ptp && (stack->mode == NT_MODE) ) {
+		msg_t *dmsg;
+		/* L2 */
+		dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
+		
+		if (stack->nst.manager_l3(&stack->nst, dmsg))
+			free_msg(dmsg);
+		
+	} else {
+		iframe_t act;
+		
+		act.prim = DL_ESTABLISH | REQUEST;
+
+		act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+		act.dinfo = 0;
+		act.len = 0;
+		return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+	}
+	
+	return 0;
+}
+
+
+int misdn_lib_get_l2_status(struct misdn_stack *stack)
+{
+	iframe_t act;
+	
+#ifdef DL_STATUS
+	act.prim = DL_STATUS | REQUEST; 
+#else
+	act.prim = DL_ESTABLISH | REQUEST;
+#endif
+	act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
+	act.dinfo = 0;
+	act.len = 0;
+	return mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+}
+
+
 static int create_process (int midev, struct misdn_bchannel *bc) {
 	iframe_t ncr;
 	int l3_id;
 	int i;
-	struct misdn_stack *stack=bc->stack;
+	struct misdn_stack *stack=get_stack_by_bc(bc);
 	int free_chan;
   
 	if (stack->mode == NT_MODE) {
@@ -508,7 +640,7 @@ static int create_process (int midev, struct misdn_bchannel *bc) {
 		cb_log(3, stack->port, " --> new_l3id %x\n",l3_id);
     
 	} else { 
-		if (stack->ptp || stack->te_choose_channel) {
+		if (stack->ptp || bc->te_choose_channel) {
 			/* we know exactly which channels are in use */
 			free_chan = find_free_chan_in_stack(stack, bc->channel_preselected?bc->channel:0);
 			if (!free_chan) return -1;
@@ -551,12 +683,15 @@ int setup_bc(struct misdn_bchannel *bc)
   
 	mISDN_pid_t pid;
 	int ret;
-	int midev=bc->stack->midev;
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
+	int midev=stack->midev;
 	int channel=bc->channel-1-(bc->channel>16);
-	int b_stid=bc->stack->b_stids[channel>=0?channel:0];
+	int b_stid=stack->b_stids[channel>=0?channel:0];
 	
 	if (bc->upset) {
-		cb_log(5, bc->stack->port, "$$$ bc already upsetted stid :%x\n", b_stid);
+		cb_log(5, stack->port, "$$$ bc already upsetted stid :%x\n", b_stid);
 		return -1;
 	}
 	
@@ -565,10 +700,10 @@ int setup_bc(struct misdn_bchannel *bc)
 		clean_up_bc(bc);
 	}
 	
-	cb_log(5, bc->stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
+	cb_log(5, stack->port, "$$$ Setting up bc with stid :%x\n", b_stid);
 	
 	if (b_stid <= 0) {
-		cb_log(0, bc->stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",bc->stack->port,channel);
+		cb_log(0, stack->port," -- Stid <=0 at the moment on port:%d channel:%d\n",stack->port,channel);
 		return 1;
 	}
 
@@ -586,7 +721,7 @@ int setup_bc(struct misdn_bchannel *bc)
 
 
 		if ( misdn_cap_is_speech(bc->capability) && !bc->nodsp && bc->async != 1) {
-			cb_log(4, bc->stack->port,"setup_bc: with dsp\n");
+			cb_log(4, stack->port,"setup_bc: with dsp\n");
 			{ 
 				int l = sizeof(li.name);
 				strncpy(li.name, "B L4", l);
@@ -596,7 +731,7 @@ int setup_bc(struct misdn_bchannel *bc)
 			li.pid.protocol[4] = ISDN_PID_L4_B_USER;
 			
 		} else {
-			cb_log(4, bc->stack->port,"setup_bc: without dsp\n");
+			cb_log(4, stack->port,"setup_bc: without dsp\n");
 			{ 
 				int l = sizeof(li.name);
 				strncpy(li.name, "B L3", l);
@@ -608,7 +743,7 @@ int setup_bc(struct misdn_bchannel *bc)
 		
 		ret = mISDN_new_layer(midev, &li);
 		if (ret <= 0) {
-			cb_log(0, bc->stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), bc->stack->port);
+			cb_log(0, stack->port,"New Layer Err: %d %s port:%d\n",ret,strerror(errno), stack->port);
 			return(-EINVAL);
 		}
       
@@ -618,18 +753,18 @@ int setup_bc(struct misdn_bchannel *bc)
 	memset(&pid, 0, sizeof(pid));
 	
 	bc->addr = ( bc->layer_id & IF_ADDRMASK) | IF_DOWN;
-	cb_log(4, bc->stack->port," --> Got Adr %x\n", bc->addr);
-	cb_log(4, bc->stack->port," --> Channel is %d\n", bc->channel);
+	cb_log(4, stack->port," --> Got Adr %x\n", bc->addr);
+	cb_log(4, stack->port," --> Channel is %d\n", bc->channel);
 	
 	
 	if (bc->async == 1 || bc->nodsp) {
-		cb_log(4, bc->stack->port," --> TRANSPARENT Mode (no DSP)\n");
+		cb_log(4, stack->port," --> TRANSPARENT Mode (no DSP)\n");
 		pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
 		pid.protocol[2] = ISDN_PID_L2_B_TRANS;
 		pid.protocol[3] = ISDN_PID_L3_B_USER;
 		pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3));
 	} else if ( misdn_cap_is_speech(bc->capability)) {
-		cb_log(4, bc->stack->port," --> TRANSPARENT Mode\n");
+		cb_log(4, stack->port," --> TRANSPARENT Mode\n");
 		pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
 		pid.protocol[2] = ISDN_PID_L2_B_TRANS;
 		pid.protocol[3] = ISDN_PID_L3_B_DSP;
@@ -637,7 +772,7 @@ int setup_bc(struct misdn_bchannel *bc)
 		pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
 		
 	} else {
-		cb_log(4, bc->stack->port," --> HDLC Mode\n");
+		cb_log(4, stack->port," --> HDLC Mode\n");
 		pid.protocol[1] = ISDN_PID_L1_B_64HDLC ;
 		pid.protocol[2] = ISDN_PID_L2_B_TRANS  ;
 		pid.protocol[3] = ISDN_PID_L3_B_USER;
@@ -648,7 +783,7 @@ int setup_bc(struct misdn_bchannel *bc)
 	
 	
 	if (ret){
-		cb_log(5, bc->stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
+		cb_log(5, stack->port,"$$$ Set Stack Err: %d %s\n",ret,strerror(errno));
     
 		mISDN_write_frame(midev, buff, bc->addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
 		return(-EINVAL);
@@ -679,32 +814,42 @@ int init_bc(struct misdn_stack *stack,  struct misdn_bchannel *bc, int midev, in
 		strncpy(bc->msn,msn, l);
 		bc->msn[l-1] = 0;
 	}
-
-
+	
+	
 	empty_bc(bc);
 	bc->upset=0;
-  
-	bc->astbuf= init_ibuffer(MISDN_IBUF_SIZE);
-	clear_ibuffer(  bc->astbuf);
-	bc->astbuf->rsem=malloc(sizeof(sem_t));
-  
-  
-	if (sem_init(bc->astbuf->rsem,1,0)<0)
-		sem_init(bc->astbuf->rsem,0,0);
-  
-  
-	bc->misdnbuf= init_ibuffer(MISDN_IBUF_SIZE);
-	clear_ibuffer(  bc->misdnbuf);
-	bc->misdnbuf->rsem=malloc(sizeof(sem_t));
-  
-	if (sem_init(bc->misdnbuf->rsem,1,0)< 0)
-		sem_init(bc->misdnbuf->rsem,0,0);
-  
-	bc->stack=stack;
-  
+	bc->port=stack->port;
+	bc->nt=stack->mode==NT_MODE?1:0;
+	
+	{
+		ibuffer_t* ibuf= init_ibuffer(MISDN_IBUF_SIZE);
+		ibuffer_t* mbuf= init_ibuffer(MISDN_IBUF_SIZE);
+
+		if (!ibuf) return -1;
+		if (!mbuf) return -1;
+		
+		clear_ibuffer( ibuf);
+		clear_ibuffer( mbuf);
+		
+		ibuf->rsem=malloc(sizeof(sem_t));
+		mbuf->rsem=malloc(sizeof(sem_t));
+		
+		bc->astbuf=ibuf;
+		bc->misdnbuf=mbuf;
+
+		if (sem_init(ibuf->rsem,1,0)<0)
+			sem_init(ibuf->rsem,0,0);
+		if (sem_init(mbuf->rsem,1,0)< 0)
+			sem_init(mbuf->rsem,0,0);
+		
+	}
+	
+	
+	
+	
 	{
 		stack_info_t *stinf;
-		ret = mISDN_get_stack_info(midev, bc->stack->port, buff, sizeof(buff));
+		ret = mISDN_get_stack_info(midev, stack->port, buff, sizeof(buff));
 		if (ret < 0) {
 			cb_log(0, port, "%s: Cannot get stack info for port:%d (ret=%d)\n", __FUNCTION__, port, ret);
 			return -1;
@@ -798,28 +943,15 @@ struct misdn_stack * stack_nt_init(struct misdn_stack *stack, int midev, int por
 		Isdnl2Init(&stack->nst);
 		Isdnl3Init(&stack->nst);
 	}
-	/* if ntmode, establish L1 to send the tei removal during start */
-	{
-		iframe_t act;
-		act.prim = PH_ACTIVATE | REQUEST; 
-		act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
-		act.dinfo = 0;
-		act.len = 0;
-		mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
-	}
 
-	stack->l2link=0;
-	if (stack->ptp) {
-		msg_t *dmsg;
-		/* L2 */
-		dmsg = create_l2msg(DL_ESTABLISH | REQUEST, 0, 0);
+	misdn_lib_get_l1_up(stack);
 
-		if (stack->nst.manager_l3(&stack->nst, dmsg))
-			free_msg(dmsg);
-
-		stack->l2link=1;
+	if (stack->ptp) {
+		misdn_lib_get_l2_up(stack);
+		stack->l2link=0;
 	}
-  
+	
+	
 	return stack;
 }
 
@@ -947,47 +1079,9 @@ struct misdn_stack* stack_te_init( int midev, int port, int ptp )
 		return NULL;
 	}
   
-
-	/* if ptp, establish link */
-	if (stack->ptp) {
-		iframe_t act;
-    
-#ifdef DL_STATUS
-		act.prim = DL_STATUS | REQUEST; 
-#else
-		act.prim = DL_ESTABLISH | REQUEST;
-#endif
-		act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
-		act.dinfo = 0;
-		act.len = 0;
-		mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
-	}
-
-#ifdef DL_STATUS
-	{ /* Pull Up L1 if we have JOLLY */ 
-		iframe_t act;
-		act.prim = PH_ACTIVATE | REQUEST; 
-		act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
-		act.dinfo = 0;
-		act.len = 0;
-		mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
-	}
-	
-	
-	if (!stack->ptp) {
-		iframe_t act;
-
-
-		act.prim = DL_STATUS | REQUEST; 
-    
-		act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN;
-		act.dinfo = 0;
-		act.len = 0;
-		mISDN_write(midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
-	}
-#endif
+	misdn_lib_get_l1_up(stack);
+	misdn_lib_get_l2_status(stack);
 	
-  
 	/*  initially, we assume that the link is NOT up */
 	stack->l2link = 0;
 	stack->l1link = 0;
@@ -1106,18 +1200,18 @@ struct misdn_bchannel *find_bc_by_addr(unsigned long addr)
 
 int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
 {
-
-	if (bc->stack->mode == TE_MODE) {
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	if (stack->mode == TE_MODE) {
 		switch (event) {
 		case EVENT_CONNECT:
 			if ( *bc->crypt_key ) {
-				cb_log(4, bc->stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+				cb_log(4, stack->port, "ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s\n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
 				
 				manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
 			}
 		case EVENT_SETUP:
 			if (bc->channel>0 && bc->channel<255)
-				set_chan_in_stack(bc->stack, bc->channel);
+				set_chan_in_stack(stack, bc->channel);
 			break;
 		case EVENT_ALERTING:
 		case EVENT_PROGRESS:
@@ -1129,9 +1223,9 @@ int handle_event ( struct misdn_bchannel *bc, enum event_e event, iframe_t *frm)
 			if (!stack) return -1;
 			
 			if (bc->channel == 0xff) {
-				bc->channel=find_free_chan_in_stack(bc->stack, 0);
+				bc->channel=find_free_chan_in_stack(stack, 0);
 				if (!bc->channel) {
-					cb_log(0, bc->stack->port, "Any Channel Requested, but we have no more!!\n");
+					cb_log(0, stack->port, "Any Channel Requested, but we have no more!!\n");
 					break;
 				}
 			} 
@@ -1160,7 +1254,7 @@ int handle_new_process(struct misdn_stack *stack, iframe_t *frm)
 		return -1;
 	}
   
-	cb_log(4, bc->stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
+	cb_log(4, stack->port, " --> new_process: New L3Id: %x\n",frm->dinfo);
 	bc->l3_id=frm->dinfo;
 	
 	if (mypid>5000) mypid=0;
@@ -1196,23 +1290,23 @@ int handle_cr ( iframe_t *frm)
 			if (!bc) {
 				cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", frm->dinfo, stack->port);
 				memset (&dummybc,0,sizeof(dummybc));
-				dummybc.stack=stack;
+				dummybc.port=stack->port;
 				dummybc.l3_id=frm->dinfo;
 				bc=&dummybc; 
 			}
       
 			if (bc) {
 				cb_log(4, stack->port, " --> lib: CLEANING UP l3id: %x\n",frm->dinfo);
-				empty_chan_in_stack(bc->stack,bc->channel);
+				empty_chan_in_stack(stack,bc->channel);
 				empty_bc(bc);
 				clean_up_bc(bc);
-				dump_chan_list(bc->stack);
+				dump_chan_list(stack);
 				bc->pid = 0;
 				cb_event(EVENT_CLEANUP, bc, glob_mgr->user_data);
 				
 				if (bc->stack_holder) {
 					cb_log(4,stack->port, "REMOVEING Holder\n");
-					stack_holder_remove( bc->stack, bc);
+					stack_holder_remove( stack, bc);
 					free(bc);
 				}
 			}
@@ -1233,13 +1327,18 @@ int handle_cr ( iframe_t *frm)
 /*Emptys bc if it's reserved (no SETUP out yet)*/
 void misdn_lib_release(struct misdn_bchannel *bc)
 {
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	if (bc->channel>=0) {
-		empty_chan_in_stack(bc->stack,bc->channel);
+		empty_chan_in_stack(stack,bc->channel);
 		empty_bc(bc);
 	}
 	clean_up_bc(bc);
 }
 
+
+
+
 int misdn_lib_get_port_up (int port) 
 { /* Pull Up L1 if we have JOLLY */ 
 	struct misdn_stack *stack;
@@ -1249,12 +1348,12 @@ int misdn_lib_get_port_up (int port)
 	     stack=stack->next) {
 		
 		if (stack->port == port) {
-			iframe_t act;
-			act.prim = DL_ESTABLISH | REQUEST; 
-			act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
-			act.dinfo = 0;
-			act.len = 0;
-			mISDN_write(stack->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
+
+			if (!stack->l1link)
+				misdn_lib_get_l1_up(stack);
+			if (!stack->l2link)
+				misdn_lib_get_l2_up(stack);
+			
 			return 0;
 		}
 	}
@@ -1287,7 +1386,7 @@ int misdn_lib_port_up(int port)
 				else
 					return 0;
 			} else {
-				if (stack->l2link)
+				if (stack->l1link)
 					return 1;
 				else
 					return 0;
@@ -1364,7 +1463,7 @@ handle_event_nt(void *dat, void *arg)
 			struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
 			int l3id = *((int *)(((u_char *)msg->data)+ mISDNUSER_HEAD_SIZE));
 			
-			cb_log(4, bc?bc->stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
+			cb_log(4, bc?stack->port:0, " --> lib: Event_ind:SETUP CONFIRM [NT] : new L3ID  is %x\n",l3id );
 	
 			if (!bc) { cb_log(4, 0, "Bc Not found (after SETUP CONFIRM)\n"); return 0; }
 	
@@ -1398,7 +1497,18 @@ handle_event_nt(void *dat, void *arg)
 		case CC_CONNECT|INDICATION:
 		{
 			struct misdn_bchannel *bc=find_bc_by_l3id(stack, hh->dinfo);
-	
+
+			if (!bc) {
+				msg_t *dmsg;
+				cb_log(0, stack->port,"!!!! We didn't found our bc, dinfo:%x port:%d\n",hh->dinfo, stack->port);
+				
+				cb_log(0, stack->port, "Releaseing call %x (No free Chan for you..)\n", hh->dinfo);
+				dmsg = create_l3msg(CC_RELEASE_COMPLETE | REQUEST,MT_RELEASE_COMPLETE, hh->dinfo,sizeof(RELEASE_COMPLETE_t), 1);
+				stack->nst.manager_l3(&stack->nst, dmsg);
+				free_msg(msg);
+				return 0;
+				
+			}
 			setup_bc(bc);
 		}
 		break;
@@ -1410,7 +1520,7 @@ handle_event_nt(void *dat, void *arg)
 				if (bc) { //repair reject bug
 					int myprocid=bc->l3_id&0x0000ffff;
 					hh->dinfo=(hh->dinfo&0xffff0000)|myprocid;
-					cb_log(4,bc->stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
+					cb_log(4,stack->port,"Repaired reject Bug, new dinfo: %x\n",hh->dinfo);
 				}
 			}
 		}
@@ -1457,7 +1567,7 @@ handle_event_nt(void *dat, void *arg)
 			if (!bc) {
 				cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
 				memset (&dummybc,0,sizeof(dummybc));
-				dummybc.stack=stack;
+				dummybc.port=stack->port;
 				dummybc.l3_id=hh->dinfo;
 				bc=&dummybc; 
 			}
@@ -1559,7 +1669,7 @@ handle_event_nt(void *dat, void *arg)
       
 			cb_log(4, stack->port, " --> Didn't found BC so temporarly creating dummy BC (l3id:%x) on port:%d\n", hh->dinfo, stack->port);
 			memset (&dummybc,0,sizeof(dummybc));
-			dummybc.stack=stack;
+			dummybc.port=stack->port;
 			dummybc.l3_id=hh->dinfo;
 			bc=&dummybc; 
 		}
@@ -1567,7 +1677,7 @@ handle_event_nt(void *dat, void *arg)
 			isdn_msg_parse_event(msgs_g,msg,bc, 1);
 			
 			if(!isdn_get_info(msgs_g,event,1)) {
-				cb_log(4, bc->stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
+				cb_log(4, stack->port, "Unknown Event Ind: prim %x dinfo %x\n",hh->prim, hh->dinfo);
 			} else {
 				cb_event(event, bc, glob_mgr->user_data);
 			}
@@ -1645,7 +1755,8 @@ static int do_tone(struct misdn_bchannel *bc, int len)
 	char buf[4096 + mISDN_HEADER_LEN];
 	iframe_t *frm= (iframe_t*)buf;
 	int  r;
-  
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	if (bc->tone == TONE_NONE) return 0;
 
 	frm->prim = DL_DATA|REQUEST;
@@ -1662,7 +1773,7 @@ static int do_tone(struct misdn_bchannel *bc, int len)
 		frm->len = TONE_425_SIZE;
 		memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
       
-		r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+		r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
 		if (r<frm->len) {
 			perror("Error written less than told bytes :(\n");
 		}
@@ -1675,14 +1786,14 @@ static int do_tone(struct misdn_bchannel *bc, int len)
 		if (bc->tone_cnt2 <= TONE_ALERT_CNT) {
 			frm->len = TONE_425_SIZE;
 			memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
-			r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+			r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
 			if (r<frm->len) {
 				perror("Error written less than told bytes :(\n");
 			}
 		} else if (bc->tone_cnt2 <= (TONE_ALERT_SILENCE_CNT)) {
 			frm->len = TONE_SILENCE_SIZE;
 			memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
-			r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+			r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
 		} else {
 			bc->tone_cnt2=-1;
 		}
@@ -1693,14 +1804,14 @@ static int do_tone(struct misdn_bchannel *bc, int len)
 		if (bc->tone_cnt2 <= TONE_BUSY_CNT) {
 			frm->len = TONE_425_SIZE;
 			memcpy(&buf[mISDN_HEADER_LEN], tone_425_flip,TONE_425_SIZE);
-			r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+			r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
 			if (r<frm->len) {
 				perror("Error written less than told bytes :(\n");
 			}
 		} else if (bc->tone_cnt2 <= (TONE_BUSY_SILENCE_CNT)) {
 			frm->len = TONE_SILENCE_SIZE;
 			memcpy(&buf[mISDN_HEADER_LEN], tone_silence_flip ,TONE_SILENCE_SIZE);
-			r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
+			r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_1SEC);
 		} else {
 			bc->tone_cnt2=-1;
 		}
@@ -1721,33 +1832,37 @@ int handle_bchan(msg_t *msg)
 {
 	iframe_t *frm= (iframe_t*)msg->data;
 	struct misdn_bchannel *bc;
-  
+	
 	bc=find_bc_by_addr(frm->addr);
-  
+	
 	if (!bc) return 0 ;
-  
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+
+	if (!stack) return 0;
+	
 	switch (frm->prim) {
 	case PH_ACTIVATE | INDICATION:
 	case DL_ESTABLISH | INDICATION:
-		cb_log(4, bc->stack->port, "BCHAN: ACT Ind\n");
+		cb_log(4, stack->port, "BCHAN: ACT Ind\n");
 		free_msg(msg);
 		return 1;    
 
 	case PH_ACTIVATE | CONFIRM:
 	case DL_ESTABLISH | CONFIRM:
-		cb_log(4, bc->stack->port, "BCHAN: bchan ACT Confirm\n");
+		cb_log(4, stack->port, "BCHAN: bchan ACT Confirm\n");
 		free_msg(msg);
 		return 1;    
 
 	case PH_DEACTIVATE | INDICATION:
 	case DL_RELEASE | INDICATION:
-		cb_log (4, bc->stack->port, "BCHAN: DeACT Ind\n");
+		cb_log (4, stack->port, "BCHAN: DeACT Ind\n");
 		free_msg(msg);
 		return 1;
     
 	case PH_DEACTIVATE | CONFIRM:
 	case DL_RELEASE | CONFIRM:
-		cb_log(4, bc->stack->port, "BCHAN: DeACT Conf\n");
+		cb_log(4, stack->port, "BCHAN: DeACT Conf\n");
 		free_msg(msg);
 		return 1;
     
@@ -1755,11 +1870,11 @@ int handle_bchan(msg_t *msg)
 	{
 		unsigned long cont = *((unsigned long *)&frm->data.p);
 		
-		cb_log(4, bc->stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+		cb_log(4, stack->port, "PH_CONTROL: port:%d channel:%d oad%d:%s dad%d:%s \n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
 
 		if ((cont&~DTMF_TONE_MASK) == DTMF_TONE_VAL) {
 			int dtmf = cont & DTMF_TONE_MASK;
-			cb_log(4, bc->stack->port, " --> DTMF TONE: %c\n",dtmf);
+			cb_log(4, stack->port, " --> DTMF TONE: %c\n",dtmf);
 			bc->dtmf=dtmf;
 			cb_event(EVENT_DTMF_TONE, bc, glob_mgr->user_data);
 	
@@ -1767,12 +1882,12 @@ int handle_bchan(msg_t *msg)
 			return 1;
 		}
 		if (cont == BF_REJECT) {
-			cb_log(4, bc->stack->port, " --> BF REJECT\n");
+			cb_log(4, stack->port, " --> BF REJECT\n");
 			free_msg(msg);
 			return 1;
 		}
 		if (cont == BF_ACCEPT) {
-			cb_log(4, bc->stack->port, " --> BF ACCEPT\n");
+			cb_log(4, stack->port, " --> BF ACCEPT\n");
 			free_msg(msg);
 			return 1;
 		}
@@ -1790,7 +1905,7 @@ int handle_bchan(msg_t *msg)
 		
 		
 #if MISDN_DEBUG
-		cb_log(0, bc->stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
+		cb_log(0, stack->port, "DL_DATA INDICATION Len %d\n", frm->len);
 #endif
 		
 		if (bc->active && frm->len > 0) {
@@ -1815,9 +1930,9 @@ int handle_bchan(msg_t *msg)
 							txfrm->addr = bc->addr; /*  | IF_DOWN; */
 							txfrm->len = frm->len;
 							ibuf_memcpy_r(&buf[mISDN_HEADER_LEN], bc->misdnbuf,frm->len);
-							cb_log(9, bc->stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
+							cb_log(9, stack->port, "Transmitting %d samples 2 misdn\n", txfrm->len);
 							
-							r=mISDN_write(bc->stack->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
+							r=mISDN_write(stack->midev, buf, txfrm->len + mISDN_HEADER_LEN, 8000 );
 							
 						}
 						
@@ -1835,14 +1950,14 @@ int handle_bchan(msg_t *msg)
 	case PH_DATA | CONFIRM:
 	case DL_DATA|CONFIRM:
 #if MISDN_DEBUG
-		cb_log(0, bc->stack->port, "Data confirmed\n");
+		cb_log(0, stack->port, "Data confirmed\n");
 #endif
 		free_msg(msg);
 		return 1;
 		break;
 	case DL_DATA|RESPONSE:
 #if MISDN_DEBUG
-		cb_log(0, bc->stack->port, "Data response\n");
+		cb_log(0, stack->port, "Data response\n");
 #endif
 		break;
     
@@ -1930,7 +2045,7 @@ int handle_frm(msg_t *msg)
 					break;
 				case RESPONSE_IGNORE_SETUP:
 					/* I think we should send CC_RELEASE_CR, but am not sure*/
-					empty_chan_in_stack(bc->stack, bc->channel);
+					empty_chan_in_stack(stack, bc->channel);
 					empty_bc(bc);
 	  
 					cb_log(0, stack->port, "GOT IGNORE SETUP: port:%d\n", frm->addr&IF_CONTRMASK);
@@ -1972,7 +2087,14 @@ int handle_l1(msg_t *msg)
 	case PH_ACTIVATE | INDICATION:
 		cb_log (1, stack->port, "L1: PH L1Link Up! port:%d\n",stack->port);
 		stack->l1link=1;
-		free_msg(msg);
+		
+		if (stack->mode==NT_MODE) {
+			
+			if (stack->nst.l1_l2(&stack->nst, msg))
+				free_msg(msg);
+		} else {
+			free_msg(msg);
+		}
 		
 		for (i=0;i<stack->b_num; i++) {
 			if (stack->bc[i].evq != EVENT_NOTHING) {
@@ -1987,14 +2109,25 @@ int handle_l1(msg_t *msg)
 	case PH_DEACTIVATE | CONFIRM:
 	case PH_DEACTIVATE | INDICATION:
 		cb_log (1, stack->port, "L1: PH L1Link Down! port:%d\n",stack->port);
-		stack->l1link=0;
+		
 		for (i=0; i<stack->b_num; i++) {
 			if (global_state == MISDN_INITIALIZED)  {
 				cb_event(EVENT_CLEANUP, &stack->bc[i], glob_mgr->user_data);
 			}
 			
-		} 
-		free_msg(msg);
+		}
+		
+		if (stack->mode==NT_MODE) {
+			if (stack->nst.l1_l2(&stack->nst, msg))
+				free_msg(msg);
+			
+		} else {
+			free_msg(msg);
+		}
+		
+		stack->l1link=0;
+		stack->l2link=0;
+		
 		return 1;
 	}
   
@@ -2013,7 +2146,7 @@ int handle_l2(msg_t *msg)
 #ifdef DL_STATUS
 	case DL_STATUS | INDICATION:
 	case DL_STATUS | CONFIRM:
-		cb_log (3, stack->port, "L1: DL_STATUS! port:%d\n", stack->port);
+		cb_log (3, stack->port, "L2: DL_STATUS! port:%d\n", stack->port);
 		
 		switch (frm->dinfo) {
 		case SDL_ESTAB:
@@ -2023,7 +2156,7 @@ int handle_l2(msg_t *msg)
 		case SDL_REL:
 			cb_log (4, stack->port, " --> SDL_REL port:%d\n", stack->port);
 			stack->l1link=0;
-			misdn_lib_get_port_up(stack->port);
+			misdn_lib_get_l2_up(stack);
 			goto dl_rel;
 		}
 		break;
@@ -2051,9 +2184,6 @@ int handle_l2(msg_t *msg)
 	{
 		cb_log (3, stack->port, "L2: L2Link Down! port:%d\n", stack->port);
 		stack->l2link=0;
-
-		if (cb_clearl3_true())
-			clear_l3(stack);
 		
 		free_msg(msg);
 		return 1;
@@ -2228,7 +2358,8 @@ struct misdn_bchannel *manager_find_bc_by_pid(int pid)
 
 struct misdn_bchannel *manager_find_bc_holded(struct misdn_bchannel* bc)
 {
-	return find_bc_holded(bc->stack);
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	return find_bc_holded(stack);
 }
 
 
@@ -2271,10 +2402,14 @@ struct misdn_bchannel* misdn_lib_get_free_bc(int port, int channel)
 void misdn_lib_log_ies(struct misdn_bchannel *bc)
 {
 	if (!bc) return;
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+
+	if (!stack) return;
 	
-	cb_log(2, bc->stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", bc->stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
+	cb_log(2, stack->port, " --> mode:%s cause:%d ocause:%d rad:%s\n", stack->mode==NT_MODE?"NT":"TE", bc->cause, bc->out_cause, bc->rad);
 	
-	cb_log(2, bc->stack->port,
+	cb_log(2, stack->port,
 	       " --> info_dad:%s onumplan:%c dnumplan:%c rnumplan:%c\n",
 	       bc->info_dad,
 	       bc->onumplan>=0?'0'+bc->onumplan:' ',
@@ -2282,11 +2417,11 @@ void misdn_lib_log_ies(struct misdn_bchannel *bc)
 	       bc->rnumplan>=0?'0'+bc->rnumplan:' '
 		);
 	
-	cb_log(2, bc->stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
+	cb_log(2, stack->port, " --> channel:%d caps:%s pi:%x keypad:%s\n", bc->channel, bearer2str(bc->capability),bc->progress_indicator, bc->keypad);
 
-	cb_log(3, bc->stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
+	cb_log(3, stack->port, " --> pid:%d addr:%x l3id:%x\n", bc->pid, bc->addr, bc->l3_id);
 
-	cb_log(4, bc->stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
+	cb_log(4, stack->port, " --> bc:%x h:%d sh:%d\n", bc, bc->holded, bc->stack_holder);
 }
 
 int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
@@ -2295,15 +2430,17 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 	int err = -1 ;
   
 	if (!bc) goto ERR; 
-
-	if ( bc->stack->mode == NT_MODE && !bc->stack->l1link) {
+	
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
+	if ( stack->mode == NT_MODE && !stack->l1link) {
 		/** Queue Event **/
 		bc->evq=event;
-		cb_log(1, bc->stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
+		cb_log(1, stack->port, "Queueing Event %s because L1 is down (btw. Activating L1)\n", isdn_get_info(msgs_g, event, 0));
 		{ /* Pull Up L1 */ 
 			iframe_t act;
 			act.prim = PH_ACTIVATE | REQUEST; 
-			act.addr = (bc->stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
+			act.addr = (stack->upper_id & IF_ADDRMASK) | IF_DOWN ;
 			act.dinfo = 0;
 			act.len = 0;
 			mISDN_write(glob_mgr->midev, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
@@ -2312,13 +2449,13 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 		return 0;
 	}
 	
-	cb_log(1, bc->stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, bc->stack->port);
+	cb_log(1, stack->port, "I SEND:%s oad:%s dad:%s port:%d\n", isdn_get_info(msgs_g, event, 0), bc->oad, bc->dad, stack->port);
 	misdn_lib_log_ies(bc);
 	
 	switch (event) {
 	case EVENT_SETUP:
 		if (create_process(glob_mgr->midev, bc)<0) {
-			cb_log(0,  bc->stack->port, " No free channel at the moment @ send_event\n");
+			cb_log(0,  stack->port, " No free channel at the moment @ send_event\n");
 			err=-ENOCHAN;
 			goto ERR;
 		}
@@ -2331,11 +2468,11 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 	case EVENT_SETUP_ACKNOWLEDGE:
 	case EVENT_RETRIEVE_ACKNOWLEDGE:
 		
-		if (bc->stack->mode == NT_MODE) {
+		if (stack->mode == NT_MODE) {
 			if (bc->channel <=0 ) { /*  else we have the channel already */
-				bc->channel = find_free_chan_in_stack(bc->stack, 0);
+				bc->channel = find_free_chan_in_stack(stack, 0);
 				if (!bc->channel) {
-					cb_log(0, bc->stack->port, " No free channel at the moment\n");
+					cb_log(0, stack->port, " No free channel at the moment\n");
 					err=-ENOCHAN;
 					goto ERR;
 				}
@@ -2348,7 +2485,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 		
 		if ( event == EVENT_CONNECT ) {
 			if ( *bc->crypt_key ) {
-				cb_log(4, bc->stack->port,  " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", bc->stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
+				cb_log(4, stack->port,  " --> ENABLING BLOWFISH port:%d channel:%d oad%d:%s dad%d:%s \n", stack->port, bc->channel, bc->onumplan,bc->oad, bc->dnumplan,bc->dad);
 				
 				manager_ph_control_block(bc,  BF_ENABLE_KEY, bc->crypt_key, strlen(bc->crypt_key) );
 			}
@@ -2359,12 +2496,12 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 				if (bc->ec_enable) manager_ec_enable(bc);
 				
 				if (bc->txgain != 0) {
-					cb_log(4, bc->stack->port,  "--> Changing txgain to %d\n", bc->txgain);
+					cb_log(4, stack->port,  "--> Changing txgain to %d\n", bc->txgain);
 					manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
 				}
 				
 				if ( bc->rxgain != 0 ) {
-					cb_log(4, bc->stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
+					cb_log(4, stack->port,  "--> Changing rxgain to %d\n", bc->rxgain);
 					manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
 				}
 			}
@@ -2383,10 +2520,10 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 		memcpy(holded_bc,bc,sizeof(struct misdn_bchannel));
 		
 		holded_bc->holded=1;
-		stack_holder_add(bc->stack,holded_bc);
+		stack_holder_add(stack,holded_bc);
 		
-		if (bc->stack->mode == NT_MODE) {
-			empty_chan_in_stack(bc->stack,bc->channel);
+		if (stack->mode == NT_MODE) {
+			empty_chan_in_stack(stack,bc->channel);
 			empty_bc(bc);
 			clean_up_bc(bc);
 		}
@@ -2403,7 +2540,7 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 		break;
 		
 	case EVENT_RELEASE_COMPLETE:
-		empty_chan_in_stack(bc->stack,bc->channel);
+		empty_chan_in_stack(stack,bc->channel);
 		empty_bc(bc);
 		clean_up_bc(bc);
 		break;
@@ -2414,11 +2551,11 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 			if (  !bc->nodsp) manager_ph_control(bc,  DTMF_TONE_START, 0);
 			if (bc->ec_enable) manager_ec_enable(bc);
 			if ( bc->txgain != 0 ) {
-				cb_log(4, bc->stack->port, "--> Changing txgain to %d\n", bc->txgain);
+				cb_log(4, stack->port, "--> Changing txgain to %d\n", bc->txgain);
 				manager_ph_control(bc, VOL_CHANGE_TX, bc->txgain);
 			}
 			if ( bc->rxgain != 0 ) {
-				cb_log(4, bc->stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
+				cb_log(4, stack->port, "--> Changing rxgain to %d\n", bc->rxgain);
 				manager_ph_control(bc, VOL_CHANGE_RX, bc->rxgain);
 			}
 		}
@@ -2429,8 +2566,8 @@ int misdn_lib_send_event(struct misdn_bchannel *bc, enum event_e event )
 	}
   
 	/* Later we should think about sending bchannel data directly to misdn. */
-	msg = isdn_msg_build_event(msgs_g, bc, event, bc->stack->mode==NT_MODE?1:0);
-	msg_queue_tail(&bc->stack->downqueue, msg);
+	msg = isdn_msg_build_event(msgs_g, bc, event, stack->mode==NT_MODE?1:0);
+	msg_queue_tail(&stack->downqueue, msg);
 	sem_post(&glob_mgr->new_msg);
   
 	return 0;
@@ -2832,8 +2969,10 @@ void manager_bchannel_activate(struct misdn_bchannel *bc)
 	msg_t *msg=alloc_msg(MAX_MSG_SIZE);
 	iframe_t *frm;
 
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	if (!msg) {
-		cb_log(0, bc->stack->port, "bchannel_activate: alloc_msg failed !");
+		cb_log(0, stack->port, "bchannel_activate: alloc_msg failed !");
 		return ;
 	}
 	
@@ -2845,7 +2984,7 @@ void manager_bchannel_activate(struct misdn_bchannel *bc)
 	
 	if (bc->active) return;
   
-	cb_log(5, bc->stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
+	cb_log(5, stack->port, "$$$ Bchan Activated addr %x\n", bc->addr);
 
 	/* activate bchannel */
 	frm->prim = DL_ESTABLISH | REQUEST;
@@ -2867,10 +3006,10 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
 {
 	iframe_t dact;
 
-  
+	struct misdn_stack *stack=get_stack_by_bc(bc);
 	if (!bc->active) return;
   
-	cb_log(5, bc->stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
+	cb_log(5, stack->port, "$$$ Bchan deActivated addr %x\n", bc->addr);
   
 	bc->tone=TONE_NONE;
 
@@ -2879,7 +3018,7 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
 	dact.dinfo = 0;
 	dact.len = 0;
   
-	mISDN_write(bc->stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
+	mISDN_write(stack->midev, &dact, mISDN_HEADER_LEN+dact.len, TIMEOUT_1SEC);
 	clear_ibuffer(bc->misdnbuf);
 	clear_ibuffer(bc->astbuf);
 	bc->active=0;
@@ -2890,7 +3029,8 @@ void manager_bchannel_deactivate(struct misdn_bchannel * bc)
 
 int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
 {
-	
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
 	if (!bc->active) return -1;   
 	
 	flip_buf_bits(data,len);
@@ -2914,10 +3054,10 @@ int manager_tx2misdn_frm(struct misdn_bchannel *bc, void *data, int len)
 		
 		
 		if ( misdn_cap_is_speech(bc->capability))
-			cb_log(4, bc->stack->port, "Writing %d bytes\n",len);
+			cb_log(4, stack->port, "Writing %d bytes\n",len);
 		
-		cb_log(9, bc->stack->port, "Wrinting %d bytes 2 mISDN\n",len);
-		r=mISDN_write(bc->stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
+		cb_log(9, stack->port, "Wrinting %d bytes 2 mISDN\n",len);
+		r=mISDN_write(stack->midev, buf, frm->len + mISDN_HEADER_LEN, TIMEOUT_INFINIT);
 	}
 
 	return 0;
@@ -2944,14 +3084,15 @@ void manager_ph_control(struct misdn_bchannel *bc, int c1, int c2)
 	unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
 	iframe_t *ctrl = (iframe_t *)buffer; /* preload data */
 	unsigned long *d = (unsigned long *)&ctrl->data.p;
-  
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	ctrl->prim = PH_CONTROL | REQUEST;
 	ctrl->addr = bc->addr;
 	ctrl->dinfo = 0;
 	ctrl->len = sizeof(unsigned long)*2;
 	*d++ = c1;
 	*d++ = c2;
-	mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+	mISDN_write(stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
 }
 
 /*
@@ -2962,6 +3103,7 @@ void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c
 	unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
 	iframe_t *ctrl = (iframe_t *)buffer;
 	unsigned long *d = (unsigned long *)&ctrl->data.p;
+	struct misdn_stack *stack=get_stack_by_bc(bc);
 	
 	ctrl->prim = PH_CONTROL | REQUEST;
 	ctrl->addr = bc->addr;
@@ -2969,7 +3111,7 @@ void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c
 	ctrl->len = sizeof(unsigned long) + c2_len;
 	*d++ = c1;
 	memcpy(d, c2, c2_len);
-	mISDN_write(bc->stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
+	mISDN_write(stack->midev, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
 }
 
 
@@ -2977,10 +3119,12 @@ void manager_ph_control_block(struct misdn_bchannel *bc, int c1, void *c2, int c
 
 void manager_clean_bc(struct misdn_bchannel *bc )
 {
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	if (bc->state == STATE_CONNECTED)
 		misdn_lib_send_event(bc,EVENT_DISCONNECT);
 
-	empty_chan_in_stack(bc->stack, bc->channel);
+	empty_chan_in_stack(stack, bc->channel);
 	empty_bc(bc);
   
 	misdn_lib_send_event(bc,EVENT_RELEASE_COMPLETE);
@@ -2990,11 +3134,10 @@ void manager_clean_bc(struct misdn_bchannel *bc )
 void stack_holder_add(struct misdn_stack *stack, struct misdn_bchannel *holder)
 {
 	struct misdn_bchannel *help;
-
-	cb_log(4,holder->stack->port, "*HOLDER: add %x\n",holder->l3_id);
+	cb_log(4,stack->port, "*HOLDER: add %x\n",holder->l3_id);
 	
 	holder->stack_holder=1;
-	
+
 	if (!stack ) return ;
 	
 	holder->next=NULL;
@@ -3020,7 +3163,7 @@ void stack_holder_remove(struct misdn_stack *stack, struct misdn_bchannel *holde
 
 	if (!holder->stack_holder) return;
 	
-	cb_log(4,holder->stack->port, "*HOLDER: remove %x\n",holder->l3_id);
+	cb_log(4,stack->port, "*HOLDER: remove %x\n",holder->l3_id);
 	if (!stack || ! stack->holding) return;
   
 	if (holder == stack->holding) {
@@ -3065,8 +3208,10 @@ struct misdn_bchannel *stack_holder_find(struct misdn_stack *stack, unsigned lon
 void manager_ec_enable(struct misdn_bchannel *bc)
 {
 	int ec_arr[2];
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
 	
-	cb_log(1, bc?bc->stack->port:0,"Sending Control ECHOCAN_ON enblock\n");
+	cb_log(1, stack?stack->port:0,"Sending Control ECHOCAN_ON enblock\n");
 	
 	switch (bc->ec_deftaps) {
 	case 4:
@@ -3078,10 +3223,10 @@ void manager_ec_enable(struct misdn_bchannel *bc)
 	case 256:
 	case 512:
 	case 1024:
-		cb_log(4, bc->stack->port, "Taps is %d\n",bc->ec_deftaps);
+		cb_log(4, stack->port, "Taps is %d\n",bc->ec_deftaps);
 		break;
 	default:
-		cb_log(0, bc->stack->port, "Taps should be power of 2\n");
+		cb_log(0, stack->port, "Taps should be power of 2\n");
 		bc->ec_deftaps=128;
 	}
 
@@ -3094,10 +3239,33 @@ void manager_ec_enable(struct misdn_bchannel *bc)
 
 void manager_ec_disable(struct misdn_bchannel *bc)
 {
-	cb_log(1, bc?bc->stack->port:0, "Sending Control ECHOCAN_OFF\n");
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
+	cb_log(1, stack?stack->port:0, "Sending Control ECHOCAN_OFF\n");
 	manager_ph_control(bc,  ECHOCAN_OFF, 0);
 }
 
 struct misdn_stack* get_misdn_stack() {
 	return glob_mgr->stack_list;
 }
+
+
+
+void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2) {
+	manager_ph_control(bc1, CMX_RECEIVE_OFF, 0);
+	manager_ph_control(bc2, CMX_RECEIVE_OFF, 0);
+	
+	manager_ph_control(bc1, CMX_CONF_JOIN, (bc1->pid<<1) +1);
+	manager_ph_control(bc2, CMX_CONF_JOIN, (bc1->pid<<1) +1);
+}
+
+void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2)
+{
+	
+	manager_ph_control(bc1, CMX_RECEIVE_ON, 0) ;
+	manager_ph_control(bc2, CMX_RECEIVE_ON, 0);
+	
+	manager_ph_control(bc1, CMX_CONF_SPLIT, (bc1->pid<<1) +1);
+	manager_ph_control(bc2, CMX_CONF_SPLIT, (bc1->pid<<1) +1);
+	
+}
diff --git a/channels/misdn/isdn_lib.h b/channels/misdn/isdn_lib.h
index a6f3a25a888b5650600d42fb2811f55fc96b7ea4..9b84ae1101ca8c6acf3a38e137c8fa66889d73cb 100755
--- a/channels/misdn/isdn_lib.h
+++ b/channels/misdn/isdn_lib.h
@@ -14,37 +14,13 @@
 #ifndef TE_LIB
 #define TE_LIB
 
-#include <mISDNlib.h>
-#include <isdn_net.h>
-#include <l3dss1.h>
-#include <net_l3.h>
-
-#include <pthread.h>
-
-#ifndef mISDNUSER_HEAD_SIZE
-
-#ifdef MISDNUSER_JOLLY
-#define mISDNUSER_HEAD_SIZE (sizeof(mISDNuser_head_t))
-#else
-#define mISDNUSER_HEAD_SIZE (sizeof(mISDN_head_t))
-#endif
-#endif
-
-#define MISDN_ASTERISK_TECH_PVT(ast) ast->tech_pvt
-#define MISDN_ASTERISK_PVT(ast) 1
-#define MISDN_ASTERISK_TYPE(ast) ast->tech->type
-
-
-/* #include "ies.h" */
-
-#define MAX_BCHANS 30
-
-
 /** For initialization usage **/
 /* typedef int ie_nothing_t ;*/
 /** end of init usage **/
 
 
+#define MAX_BCHANS 30
+
 enum bc_state_e {
 	STATE_NOTHING=0,
 	STATE_NULL,
@@ -173,12 +149,20 @@ enum facility_type {
 	FACILITY_CALLDEFLECT
 };
 
+
+
+
 struct misdn_bchannel {
+
+	int nt;
+	int port;
 	/** init stuff **/
 	int b_stid;
 	/* int b_addr; */
 	int layer_id;
-  
+
+
+	
 	/** var stuff**/
 	int l3_id;
 	int pid;
@@ -194,11 +178,13 @@ struct misdn_bchannel {
 	int bframe_len;
 	int time_usec;
 	
-	sem_t astsem;
-	sem_t misdnsem;
-	ibuffer_t *astbuf;
-	ibuffer_t *misdnbuf;
-  
+	
+	void *astbuf;
+	void *misdnbuf;
+	
+
+	int te_choose_channel;
+	
 	/* dtmf digit */
 	int dtmf;
 	int send_dtmf;
@@ -297,58 +283,8 @@ struct misdn_bchannel {
 	int rxgain;
   
 	struct misdn_bchannel *next;
-	struct misdn_stack *stack;
 };
 
-struct misdn_stack {
-	/** is first element because &nst equals &mISDNlist **/
-	net_stack_t nst;
-	manager_t mgr;
-  
-	int d_stid;
-  
-	int b_num;
-  
-	int b_stids[MAX_BCHANS + 1];
-  
-	int ptp;
-	int lower_id;
-	int upper_id;
-  
-	int l2link;
-  
-	time_t l2establish;
-  
-	int l1link;
-	int midev;
-  
-	enum mode_e {NT_MODE, TE_MODE} mode;
-	int pri;
-  
-
-	int procids[0x100+1];
-
-	msg_queue_t downqueue;
-	int busy;
-  
-	int port;
-	struct misdn_bchannel bc[MAX_BCHANS + 1];
-  
-	struct misdn_bchannel* bc_list; 
-  
-	int channels[MAX_BCHANS + 1];
-
-  
-  
-	int te_choose_channel;
-  
-
-	struct misdn_bchannel *holding; /* Queue which holds holded channels :) */
-  
-	struct misdn_stack *next;
-}; 
-
-struct misdn_stack* get_misdn_stack( void );
 
 enum event_response_e (*cb_event) (enum event_e event, struct misdn_bchannel *bc, void *user_data);
 void (*cb_log) (int level, int port, char *tmpl, ...);
@@ -400,35 +336,26 @@ int misdn_inband_avail(struct misdn_bchannel *bc);
 int misdn_lib_send_facility(struct misdn_bchannel *bc, enum facility_type fac, void *data);
 
 
-struct isdn_msg {
-	unsigned long misdn_msg;
-  
-	enum layer_e layer;
-	enum event_e event;
-  
-	void (*msg_parser)(struct isdn_msg *msgs, msg_t *msg, struct misdn_bchannel *bc, int nt);
-	msg_t *(*msg_builder)(struct isdn_msg *msgs, struct misdn_bchannel *bc, int nt);
-	void (*msg_printer)(struct isdn_msg *msgs);
-  
-	char *info;
-  
-} ; 
-
-
-
-
+void manager_ec_enable(struct misdn_bchannel *bc);
+void manager_ec_disable(struct misdn_bchannel *bc);
 
+void get_show_stack_details(int port, char *buf);
 
 
+/** Ibuf interface **/
+int misdn_ibuf_usedcount(void *buf);
+int misdn_ibuf_freecount(void *buf);
+void misdn_ibuf_memcpy_r(char *to, void *from, int len);
+void misdn_ibuf_memcpy_w(void *buf, char *from, int len);
 
-void manager_ec_enable(struct misdn_bchannel *bc);
-void manager_ec_disable(struct misdn_bchannel *bc);
+/** Ibuf interface End **/
 
 
-/* for isdn_msg_parser.c */
-msg_t *create_l3msg(int prim, int mt, int dinfo , int size, int nt);
+void misdn_lib_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2);
+void misdn_lib_split_bridge( struct misdn_bchannel * bc1, struct misdn_bchannel *bc2);
 
 
+int misdn_lib_is_ptp(int port);
 
 #define PRI_TRANS_CAP_SPEECH                                    0x0
 #define PRI_TRANS_CAP_DIGITAL                                   0x08
diff --git a/channels/misdn/isdn_lib_intern.h b/channels/misdn/isdn_lib_intern.h
new file mode 100755
index 0000000000000000000000000000000000000000..471648a68563a2aa8a8d2107b8e139780a6fc3c2
--- /dev/null
+++ b/channels/misdn/isdn_lib_intern.h
@@ -0,0 +1,99 @@
+#ifndef ISDN_LIB_INTERN
+#define ISDN_LIB_INTER
+
+
+#include <mISDNlib.h>
+#include <isdn_net.h>
+#include <l3dss1.h>
+#include <net_l3.h>
+
+#include <pthread.h>
+
+#include "isdn_lib.h"
+
+
+
+
+
+#ifndef mISDNUSER_HEAD_SIZE
+
+#ifdef MISDNUSER_JOLLY
+#define mISDNUSER_HEAD_SIZE (sizeof(mISDNuser_head_t))
+#else
+#define mISDNUSER_HEAD_SIZE (sizeof(mISDN_head_t))
+#endif
+#endif
+
+
+ibuffer_t *astbuf;
+ibuffer_t *misdnbuf;
+
+
+struct isdn_msg {
+	unsigned long misdn_msg;
+  
+	enum layer_e layer;
+	enum event_e event;
+  
+	void (*msg_parser)(struct isdn_msg *msgs, msg_t *msg, struct misdn_bchannel *bc, int nt);
+	msg_t *(*msg_builder)(struct isdn_msg *msgs, struct misdn_bchannel *bc, int nt);
+	void (*msg_printer)(struct isdn_msg *msgs);
+  
+	char *info;
+  
+} ; 
+
+/* for isdn_msg_parser.c */
+msg_t *create_l3msg(int prim, int mt, int dinfo , int size, int nt);
+
+
+
+struct misdn_stack {
+	/** is first element because &nst equals &mISDNlist **/
+	net_stack_t nst;
+	manager_t mgr;
+  
+	int d_stid;
+  
+	int b_num;
+  
+	int b_stids[MAX_BCHANS + 1];
+  
+	int ptp;
+	int lower_id;
+	int upper_id;
+  
+	int l2link;
+  
+	time_t l2establish;
+  
+	int l1link;
+	int midev;
+  
+	enum mode_e {NT_MODE, TE_MODE} mode;
+	
+	int pri;
+  
+
+	int procids[0x100+1];
+
+	msg_queue_t downqueue;
+	int busy;
+  
+	int port;
+	struct misdn_bchannel bc[MAX_BCHANS + 1];
+  
+	struct misdn_bchannel* bc_list; 
+  
+	int channels[MAX_BCHANS + 1];
+
+  
+	struct misdn_bchannel *holding; /* Queue which holds holded channels :) */
+  
+	struct misdn_stack *next;
+}; 
+
+
+struct misdn_stack* get_stack_by_bc(struct misdn_bchannel *bc);
+
+#endif
diff --git a/channels/misdn/isdn_msg_parser.c b/channels/misdn/isdn_msg_parser.c
index 0dd8d4cc18359bd8a2ae44558829bcda555155cb..4c8d95208b44379dff6fa34c65fd59437ced8c03 100755
--- a/channels/misdn/isdn_msg_parser.c
+++ b/channels/misdn/isdn_msg_parser.c
@@ -20,6 +20,8 @@ void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel
 {
 	int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
 	CALL_PROCEEDING_t *proceeding=(CALL_PROCEEDING_t*)((unsigned long)msg->data+ HEADER_LEN);
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 	{
 		int  exclusive, channel;
 		dec_ie_channel_id(proceeding->CHANNEL_ID, (Q931_info_t *)proceeding, &exclusive, &channel, nt,bc);
@@ -28,7 +30,7 @@ void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel
 			channel=-1;
     
 		/*  ALERT: is that everytime true ?  */
-		if (channel > 0 && bc->stack->mode == NT_MODE) 
+		if (channel > 0 && stack->mode == NT_MODE) 
 			bc->channel = channel;
 	}
 	
@@ -37,8 +39,6 @@ void parse_proceeding (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel
 #if DEBUG 
 	printf("Parsing PROCEEDING Msg\n"); 
 #endif
-
- 
 }
 msg_t *build_proceeding (struct isdn_msg msgs[], struct misdn_bchannel *bc, int nt)
 {
@@ -819,7 +819,9 @@ void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *b
 {
 	int HEADER_LEN = nt?mISDNUSER_HEAD_SIZE:mISDN_HEADER_LEN;
 	RESTART_t *restart=(RESTART_t*)((unsigned long)(msg->data+HEADER_LEN));
-  
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 #if DEBUG 
 	printf("Parsing RESTART Msg\n");
 #endif
@@ -829,7 +831,7 @@ void parse_restart (struct isdn_msg msgs[], msg_t *msg, struct misdn_bchannel *b
 		dec_ie_channel_id(restart->CHANNEL_ID, (Q931_info_t *)restart, &exclusive, &channel, nt,bc);
 		if (channel==0xff) /* any channel */
 			channel=-1;
-		cb_log(0, bc->stack->port, "CC_RESTART Request on channel:%d on port:%d\n",bc->stack->port);
+		cb_log(0, stack->port, "CC_RESTART Request on channel:%d on port:%d\n",stack->port);
 	}
   
  
@@ -892,6 +894,9 @@ void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bc
 	RELEASE_COMPLETE_t *release_complete=(RELEASE_COMPLETE_t*)((unsigned long)(msg->data+HEADER_LEN));
 	int location;
 	iframe_t *frm = (iframe_t*) msg->data;
+
+	struct misdn_stack *stack=get_stack_by_bc(bc);
+	
 #ifdef MISDNUSER_JOLLY
 	mISDNuser_head_t *hh;
 	hh=(mISDNuser_head_t*)msg->data;
@@ -902,12 +907,12 @@ void parse_release_complete (struct isdn_msg msgs[], msg_t *msg, struct misdn_bc
   
 	if (nt) {
 		if (hh->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
-			cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] port:%d\n",bc->stack->port);
+			cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [NT] port:%d\n",stack->port);
 			return;
 		}
 	} else {
 		if (frm->prim == (CC_RELEASE_COMPLETE|CONFIRM)) {
-			cb_log(0, bc->stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] port:%d\n",bc->stack->port);
+			cb_log(0, stack->port, "CC_RELEASE_COMPLETE|CONFIRM [TE] port:%d\n",stack->port);
 			return;
 		}
 	}
diff --git a/configs/misdn.conf.sample b/configs/misdn.conf.sample
index fcd4c416b996166cbff751c74449847f4ece528f..8957e2fa62438c1e174d5ba635a06596fce2f297 100755
--- a/configs/misdn.conf.sample
+++ b/configs/misdn.conf.sample
@@ -217,7 +217,7 @@ use_callingpres=yes
 ; be aware, move this setting only to outgoing portgroups!
 ; A value of zero turns echocancellation off.
 ;
-; possible values are: 0,32,64,128.256,yes(=128),no(=0)
+; possible values are: 0,32,64,128,256,yes(=128),no(=0)
 ;
 ; default value: no
 ;