Index: app_queue.c
===================================================================
--- app_queue.c	(revision 8001)
+++ app_queue.c	(working copy)
@@ -259,7 +259,8 @@
    use it not only for keeping track of what is in use but
    also for keeping track of who we're dialing. */
 
-struct localuser {
+struct queue_localuser {
+	struct queue_localuser *q_next;
 	struct ast_channel *chan;
 	char interface[256];
 	int stillgoing;
@@ -267,7 +268,6 @@
 	int oldstatus;
 	time_t lastcall;
 	struct member *member;
-	struct localuser *next;
 };
 
 LOCAL_USER_DECL;
@@ -1236,16 +1236,16 @@
 }
 
 /* Hang up a list of outgoing calls */
-static void hangupcalls(struct localuser *outgoing, struct ast_channel *exception)
+static void hangupcalls(struct queue_localuser *outgoing, struct ast_channel *exception)
 {
-	struct localuser *oo;
+	struct queue_localuser *oo;
 
 	while(outgoing) {
 		/* Hangup any existing lines we have open */
 		if (outgoing->chan && (outgoing->chan != exception))
 			ast_hangup(outgoing->chan);
 		oo = outgoing;
-		outgoing=outgoing->next;
+		outgoing=outgoing->q_next;
 		free(oo);
 	}
 }
@@ -1257,8 +1257,7 @@
 	/* Since a reload could have taken place, we have to traverse the list to
 		be sure it's still valid */
 	ast_mutex_lock(&q->lock);
-	cur = q->members;
-	while(cur) {
+	for (cur = q->members; cur; cur = cur->next) {
 		if (member == cur) {
 			cur->status = status;
 			if (!q->maskmemberstatus) {
@@ -1276,7 +1275,6 @@
 			}
 			break;
 		}
-		cur = cur->next;
 	}
 	ast_mutex_unlock(&q->lock);
 	return 0;
@@ -1329,13 +1327,22 @@
 	return found;
 }
 
-static int ring_entry(struct queue_ent *qe, struct localuser *tmp, int *busies)
+/*! \brief common hangup actions */
+static void do_hang(struct queue_localuser *o)
 {
+	o->stillgoing = 0;
+	ast_hangup(o->chan);
+	o->chan = NULL;
+}
+
+static int ring_entry(struct queue_ent *qe, struct queue_localuser *tmp, int *busies)
+{
 	int res;
 	int status;
 	char tech[256];
 	char *location;
 
+	/* on entry here, we know that tmp->chan == NULL */
 	if (qe->parent->wrapuptime && (time(NULL) - tmp->lastcall < qe->parent->wrapuptime)) {
 		if (option_debug)
 			ast_log(LOG_DEBUG, "Wrapuptime not yet expired for %s\n", tmp->interface);
@@ -1417,9 +1424,7 @@
 			ast_log(LOG_DEBUG, "ast call on peer returned %d\n", res);
 		else if (option_verbose > 2)
 			ast_verbose(VERBOSE_PREFIX_3 "Couldn't call %s\n", tmp->interface);
-		ast_hangup(tmp->chan);
-		tmp->chan = NULL;
-		tmp->stillgoing = 0;
+		do_hang(tmp);
 		(*busies)++;
 		return 0;
 	} else {
@@ -1443,69 +1448,58 @@
 	return 1;
 }
 
-static int ring_one(struct queue_ent *qe, struct localuser *outgoing, int *busies)
+/*! \brief find the entry with the best metric, or NULL */
+static struct queue_localuser *find_best(struct queue_localuser *outgoing)
 {
-	struct localuser *cur;
-	struct localuser *best;
-	int bestmetric=0;
+	struct queue_localuser *best = NULL, *cur;
 
-	do {
-		best = NULL;
-		cur = outgoing;
-		while(cur) {
-			if (cur->stillgoing &&					/* Not already done */
-				!cur->chan &&					/* Isn't already going */
-				(!best || (cur->metric < bestmetric))) {	/* We haven't found one yet, or it's better */
-					bestmetric = cur->metric;
-					best = cur;
-			}
-			cur = cur->next;
+	for (cur = outgoing; cur; cur = cur->q_next) {
+		if (cur->stillgoing &&					/* Not already done */
+			!cur->chan &&					/* Isn't already going */
+			(!best || cur->metric < best->metric)) {	/* We haven't found one yet, or it's better */
+				best = cur;
 		}
-		if (best) {
-			if (!qe->parent->strategy) {
-				/* Ring everyone who shares this best metric (for ringall) */
-				cur = outgoing;
-				while(cur) {
-					if (cur->stillgoing && !cur->chan && (cur->metric <= bestmetric)) {
-						if (option_debug)
-							ast_log(LOG_DEBUG, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
-						ring_entry(qe, cur, busies);
-					}
-					cur = cur->next;
+	}
+	return best;
+}
+
+static int ring_one(struct queue_ent *qe, struct queue_localuser *outgoing, int *busies)
+{
+	int ret = 0;
+
+	while (ret == 0) {
+		struct queue_localuser *best = find_best(outgoing);
+		if (!best) {
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Nobody left to try ringing in queue\n");
+			break;
+		}
+		if (!qe->parent->strategy) {
+			struct queue_localuser *cur;
+			/* Ring everyone who shares this best metric (for ringall) */
+			for (cur = outgoing; cur; cur = cur->q_next) {
+				if (cur->stillgoing && !cur->chan && cur->metric <= best->metric) {
+					if (option_debug)
+						ast_log(LOG_DEBUG, "(Parallel) Trying '%s' with metric %d\n", cur->interface, cur->metric);
+					ring_entry(qe, cur, busies);
 				}
-			} else {
-				/* Ring just the best channel */
-				if (option_debug)
-					ast_log(LOG_DEBUG, "Trying '%s' with metric %d\n", best->interface, best->metric);
-				ring_entry(qe, best, busies);
 			}
+		} else {
+			/* Ring just the best channel */
+			if (option_debug)
+				ast_log(LOG_DEBUG, "Trying '%s' with metric %d\n", best->interface, best->metric);
+			ring_entry(qe, best, busies);
 		}
-	} while (best && !best->chan);
-	if (!best) {
-		if (option_debug)
-			ast_log(LOG_DEBUG, "Nobody left to try ringing in queue\n");
-		return 0;
+		if (best->chan) /* break out with result = 1 */
+			ret = 1;
 	}
-	return 1;
+	return ret;
 }
 
-static int store_next(struct queue_ent *qe, struct localuser *outgoing)
+static int store_next(struct queue_ent *qe, struct queue_localuser *outgoing)
 {
-	struct localuser *cur;
-	struct localuser *best;
-	int bestmetric=0;
+	struct queue_localuser *best = find_best(outgoing);
 
-	best = NULL;
-	cur = outgoing;
-	while(cur) {
-		if (cur->stillgoing &&					/* Not already done */
-			!cur->chan &&					/* Isn't already going */
-			(!best || (cur->metric < bestmetric))) {	/* We haven't found one yet, or it's better */
-				bestmetric = cur->metric;
-				best = cur;
-		}
-		cur = cur->next;
-	}
 	if (best) {
 		/* Ring just the best channel */
 		if (option_debug)
@@ -1592,32 +1586,10 @@
 
 #define AST_MAX_WATCHERS 256
 
-#define BUILD_WATCHERS do { \
-		o = outgoing; \
-		found = -1; \
-		pos = 1; \
-		numlines = 0; \
-		watchers[0] = in; \
-		while(o) { \
-			/* Keep track of important channels */ \
-			if (o->stillgoing) { \
-				stillgoing = 1; \
-				if (o->chan) { \
-					watchers[pos++] = o->chan; \
-					found = 1; \
-				} \
-			} \
-			o = o->next; \
-			numlines++; \
-		} \
-	} while(0)
-	
-static struct localuser *wait_for_answer(struct queue_ent *qe, struct localuser *outgoing, int *to, char *digit, int prebusies, int caller_disconnect)
+static struct queue_localuser *wait_for_answer(struct queue_ent *qe, struct queue_localuser *outgoing, int *to, char *digit, int prebusies, int caller_disconnect)
 {
 	char *queue = qe->parent->name;
-	struct localuser *o;
-	int found;
-	int numlines;
+	struct queue_localuser *o;
 	int status;
 	int sentringing = 0;
 	int numbusies = prebusies;
@@ -1625,21 +1597,34 @@
 	int stillgoing = 0;
 	int orig = *to;
 	struct ast_frame *f;
-	struct localuser *peer = NULL;
-	struct ast_channel *watchers[AST_MAX_WATCHERS];
-	int pos;
+	struct queue_localuser *peer = NULL;
 	struct ast_channel *winner;
 	struct ast_channel *in = qe->chan;
 	
 	while(*to && !peer) {
-		BUILD_WATCHERS;
-		if ((found < 0) && stillgoing && !qe->parent->strategy) {
+		int numlines, retry, pos = 1;
+		struct ast_channel *watchers[AST_MAX_WATCHERS];
+		watchers[0] = in;
+
+		for (retry = 0; retry < 2; retry++) {
+			numlines = 0;
+			for (o = outgoing; o; o = o->q_next) { /* Keep track of important channels */
+				if (o->stillgoing) {	/* Keep track of important channels */
+					stillgoing = 1;
+					if (o->chan)
+						watchers[pos++] = o->chan;
+				}
+				numlines++;
+			}
+			if (pos > 1 /* found */ || !stillgoing /* nobody listening */ ||
+					 qe->parent->strategy /* ring would not be delivered */)
+				break;
 			/* On "ringall" strategy we only move to the next penalty level
 			   when *all* ringing phones are done in the current penalty level */
 			ring_one(qe, outgoing, &numbusies);
-			BUILD_WATCHERS;
+			/* and retry... */
 		}
-		if (found < 0) {
+		if (pos == 1 /* not found */) {
 			if (numlines == (numbusies + numnochan)) {
 				ast_log(LOG_DEBUG, "Everyone is busy at this time\n");
 			} else {
@@ -1649,8 +1634,7 @@
 			return NULL;
 		}
 		winner = ast_waitfor_n(watchers, pos, to);
-		o = outgoing;
-		while(o) {
+		for (o = outgoing; o; o = o->q_next) {
 			if (o->stillgoing && (o->chan) &&  (o->chan->_state == AST_STATE_UP)) {
 				if (!peer) {
 					if (option_verbose > 2)
@@ -1664,8 +1648,7 @@
 					char *tech;
 					ast_copy_string(tmpchan, o->chan->call_forward, sizeof(tmpchan));
 					if ((stuff = strchr(tmpchan, '/'))) {
-						*stuff = '\0';
-						stuff++;
+						*stuff++ = '\0';
 						tech = tmpchan;
 					} else {
 						snprintf(tmpchan, sizeof(tmpchan), "%s@%s", o->chan->call_forward, o->chan->context);
@@ -1707,10 +1690,8 @@
 						if (in->cid.cid_ani) {
 							if (o->chan->cid.cid_ani)
 								free(o->chan->cid.cid_ani);
-							o->chan->cid.cid_ani = malloc(strlen(in->cid.cid_ani) + 1);
-							if (o->chan->cid.cid_ani)
-								strncpy(o->chan->cid.cid_ani, in->cid.cid_ani, strlen(in->cid.cid_ani) + 1);
-							else
+							o->chan->cid.cid_ani = strdup(in->cid.cid_ani);
+							if (!o->chan->cid.cid_ani)
 								ast_log(LOG_WARNING, "Out of memory\n");
 						}
 						if (o->chan->cid.cid_rdnis) 
@@ -1721,9 +1702,7 @@
 							o->chan->cid.cid_rdnis = strdup(in->exten);
 						if (ast_call(o->chan, tmpchan, 0)) {
 							ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
-							o->stillgoing = 0;
-							ast_hangup(o->chan);
-							o->chan = NULL;
+							do_hang(o);
 							numnochan++;
 						}
 					}
@@ -1746,11 +1725,9 @@
 						case AST_CONTROL_BUSY:
 							if (option_verbose > 2)
 								ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", o->chan->name);
-							o->stillgoing = 0;
 							if (in->cdr)
 								ast_cdr_busy(in->cdr);
-							ast_hangup(o->chan);
-							o->chan = NULL;
+							do_hang(o);
 							if (qe->parent->strategy) {
 								if (qe->parent->timeoutrestart)
 									*to = orig;
@@ -1761,11 +1738,9 @@
 						case AST_CONTROL_CONGESTION:
 							if (option_verbose > 2)
 								ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", o->chan->name);
-							o->stillgoing = 0;
 							if (in->cdr)
 								ast_cdr_busy(in->cdr);
-							ast_hangup(o->chan);
-							o->chan = NULL;
+							do_hang(o);
 							if (qe->parent->strategy) {
 								if (qe->parent->timeoutrestart)
 									*to = orig;
@@ -1792,9 +1767,7 @@
 					}
 					ast_frfree(f);
 				} else {
-					o->stillgoing = 0;
-					ast_hangup(o->chan);
-					o->chan = NULL;
+					do_hang(o);
 					if (qe->parent->strategy) {
 						if (qe->parent->timeoutrestart)
 							*to = orig;
@@ -1802,7 +1775,6 @@
 					}
 				}
 			}
-			o = o->next;
 		}
 		if (winner == in) {
 			f = ast_read(in);
@@ -1937,7 +1909,7 @@
 	return 0;
 }
 
-static int calc_metric(struct ast_call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct localuser *tmp)
+static int calc_metric(struct ast_call_queue *q, struct member *mem, int pos, struct queue_ent *qe, struct queue_localuser *tmp)
 {
 	if (mem->penalty > qe->max_penalty)
 		return -1;
@@ -1995,7 +1967,7 @@
 static int try_calling(struct queue_ent *qe, const char *options, char *announceoverride, const char *url, int *go_on)
 {
 	struct member *cur;
-	struct localuser *outgoing=NULL, *tmp = NULL;
+	struct queue_localuser *outgoing=NULL; /* the queue we are building */
 	int to;
 	char restofit[AST_MAX_EXTENSION];
 	char oldexten[AST_MAX_EXTENSION]="";
@@ -2004,7 +1976,7 @@
 	char *newnum;
 	struct ast_channel *peer;
 	struct ast_channel *which;
-	struct localuser *lpeer;
+	struct queue_localuser *lpeer;
 	struct member *member;
 	int res = 0, bridge = 0;
 	int numbusies = 0;
@@ -2062,8 +2034,8 @@
 	if (!ast_strlen_zero(announceoverride))
 		announce = announceoverride;
 
-	while(cur) {
-		tmp = malloc(sizeof(*tmp));
+	for (;cur; cur = cur->next) {
+		struct queue_localuser *tmp = calloc(1, sizeof(*tmp));
 		if (!tmp) {
 			ast_mutex_unlock(&qe->parent->lock);
 			if (use_weight) 
@@ -2071,7 +2043,6 @@
 			ast_log(LOG_WARNING, "Out of memory\n");
 			goto out;
 		}
-		memset(tmp, 0, sizeof(*tmp));
 		tmp->stillgoing = -1;
 		if (option_debug) {
 			if (url)
@@ -2098,7 +2069,7 @@
 			/* Put them in the list of outgoing thingies...  We're ready now. 
 			   XXX If we're forcibly removed, these outgoing calls won't get
 			   hung up XXX */
-			tmp->next = outgoing;
+			tmp->q_next = outgoing;
 			outgoing = tmp;		
 			/* If this line is up, don't try anybody else */
 			if (outgoing->chan && (outgoing->chan->_state == AST_STATE_UP))
@@ -2106,13 +2077,8 @@
 		} else {
 			free(tmp);
 		}
-
-		cur = cur->next;
 	}
-	if (qe->parent->timeout)
-		to = qe->parent->timeout * 1000;
-	else
-		to = -1;
+	to = (qe->parent->timeout) ? qe->parent->timeout * 1000 : -1;
 	ring_one(qe, outgoing, &numbusies);
 	ast_mutex_unlock(&qe->parent->lock);
 	if (use_weight) 
@@ -2123,10 +2089,7 @@
 		store_next(qe, outgoing);
 	}
 	ast_mutex_unlock(&qe->parent->lock);
-	if (lpeer)
-		peer = lpeer->chan;
-	else
-		peer = NULL;
+	peer = lpeer ? lpeer->chan : NULL;
 	if (!peer) {
 		if (to) {
 			/* Musta gotten hung up */
@@ -2137,9 +2100,7 @@
 		}
 		if (option_debug)
 			ast_log(LOG_DEBUG, "%s: Nobody answered.\n", qe->chan->name);
-		goto out;
-	}
-	if (peer) {
+	} else { /* peer is valid */
 		/* Ah ha!  Someone answered within the desired timeframe.  Of course after this
 		   we will always return with -1 so that it is hung up properly after the 
 		   conversation.  */
@@ -2299,7 +2260,7 @@
 			res = 1; /* JDG: bridge successfull, leave app_queue */
 		else 
 			res = bridge; /* bridge error, stay in the queue */
-	}	
+	}
 out:
 	hangupcalls(outgoing, NULL);
 	return res;
@@ -2593,7 +2554,7 @@
 
 static int pqm_exec(struct ast_channel *chan, void *data)
 {
-	struct localuser *u;
+	struct localuser *lu;
 	char *parse;
 	int priority_jump = 0;
 	AST_DECLARE_APP_ARGS(args,
@@ -2607,11 +2568,11 @@
 		return -1;
 	}
 
-	LOCAL_USER_ADD(u);
+	LOCAL_USER_ADD(lu);
 
 	if (!(parse = ast_strdupa(data))) {
 		ast_log(LOG_WARNING, "Memory Error!\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2624,7 +2585,7 @@
 
 	if (ast_strlen_zero(args.interface)) {
 		ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename]|interface[|options])\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2633,23 +2594,23 @@
 		if (priority_jump || ast_opt_priority_jumping) {
 			if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
 				pbx_builtin_setvar_helper(chan, "PQMSTATUS", "NOTFOUND");
-				LOCAL_USER_REMOVE(u);
+				LOCAL_USER_REMOVE(lu);
 				return 0;
 			}
 		}
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		pbx_builtin_setvar_helper(chan, "PQMSTATUS", "NOTFOUND");
 		return -1;
 	}
 
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	pbx_builtin_setvar_helper(chan, "PQMSTATUS", "PAUSED");
 	return 0;
 }
 
 static int upqm_exec(struct ast_channel *chan, void *data)
 {
-	struct localuser *u;
+	struct localuser *lu;
 	char *parse;
 	int priority_jump = 0;
 	AST_DECLARE_APP_ARGS(args,
@@ -2663,11 +2624,11 @@
 		return -1;
 	}
 
-	LOCAL_USER_ADD(u);
+	LOCAL_USER_ADD(lu);
 
 	if (!(parse = ast_strdupa(data))) {
 		ast_log(LOG_WARNING, "Memory Error!\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2680,7 +2641,7 @@
 
 	if (ast_strlen_zero(args.interface)) {
 		ast_log(LOG_WARNING, "Missing interface argument to PauseQueueMember ([queuename]|interface[|options])\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2689,16 +2650,16 @@
 		if (priority_jump || ast_opt_priority_jumping) {
 			if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
 				pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "NOTFOUND");
-				LOCAL_USER_REMOVE(u);
+				LOCAL_USER_REMOVE(lu);
 				return 0;
 			}
 		}
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "NOTFOUND");
 		return -1;
 	}
 
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	pbx_builtin_setvar_helper(chan, "UPQMSTATUS", "UNPAUSED");
 	return 0;
 }
@@ -2706,7 +2667,7 @@
 static int rqm_exec(struct ast_channel *chan, void *data)
 {
 	int res=-1;
-	struct localuser *u;
+	struct localuser *lu;
 	char *parse, *temppos = NULL;
 	int priority_jump = 0;
 	AST_DECLARE_APP_ARGS(args,
@@ -2721,11 +2682,11 @@
 		return -1;
 	}
 
-	LOCAL_USER_ADD(u);
+	LOCAL_USER_ADD(lu);
 
 	if (!(parse = ast_strdupa(data))) {
 		ast_log(LOG_WARNING, "Memory Error!\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2766,14 +2727,14 @@
 		break;
 	}
 
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	return res;
 }
 
 static int aqm_exec(struct ast_channel *chan, void *data)
 {
 	int res=-1;
-	struct localuser *u;
+	struct localuser *lu;
 	char *parse, *temppos = NULL;
 	int priority_jump = 0;
 	AST_DECLARE_APP_ARGS(args,
@@ -2789,11 +2750,11 @@
 		return -1;
 	}
 
-	LOCAL_USER_ADD(u);
+	LOCAL_USER_ADD(lu);
 
 	if (!(parse = ast_strdupa(data))) {
 		ast_log(LOG_WARNING, "Memory Error!\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return -1;
 	}
 
@@ -2842,7 +2803,7 @@
 		break;
 	}
 
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	return res;
 }
 
@@ -2850,7 +2811,7 @@
 {
 	int res=-1;
 	int ringing=0;
-	struct localuser *u;
+	struct localuser *lu;
 	char *queuename;
 	char info[512];
 	char *info_ptr = info;
@@ -2875,7 +2836,7 @@
 		return -1;
 	}
 
-	LOCAL_USER_ADD(u);
+	LOCAL_USER_ADD(lu);
 
 	/* Setup our queue entry */
 	memset(&qe, 0, sizeof(qe));
@@ -3109,7 +3070,7 @@
 		set_queue_result(chan, reason);
 		res = 0;
 	}
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	return res;
 }
 
@@ -3117,16 +3078,16 @@
 {
 	int count = 0;
 	struct ast_call_queue *q;
-	struct localuser *u;
+	struct localuser *lu;
 	struct member *m;
 
-	LOCAL_USER_ACF_ADD(u);
+	LOCAL_USER_ACF_ADD(lu);
 
 	ast_copy_string(buf, "0", len);
 	
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_ERROR, "QUEUEAGENTCOUNT requires an argument: queuename\n");
-		LOCAL_USER_REMOVE(u);
+		LOCAL_USER_REMOVE(lu);
 		return buf;
 	}
 
@@ -3153,7 +3114,7 @@
 	}
 
 	snprintf(buf, len, "%d", count);
-	LOCAL_USER_REMOVE(u);
+	LOCAL_USER_REMOVE(lu);
 	return buf;
 }
 
@@ -3191,8 +3152,8 @@
 		q = q->next;
 	}
 	/* Chug through config file */
-	cat = ast_category_browse(cfg, NULL);
-	while(cat) {
+	cat = NULL;
+	while ((cat = ast_category_browse(cfg, cat)) ) {
 		if (!strcasecmp(cat, "general")) {	
 			/* Initialize global settings */
 			queue_persistent_members = 0;
@@ -3200,11 +3161,9 @@
 				queue_persistent_members = ast_true(general_val);
 		} else {	/* Define queue */
 			/* Look for an existing one */
-			q = queues;
-			while(q) {
+			for (q = queues; q ; q = q->next) {
 				if (!strcmp(q->name, cat))
 					break;
-				q = q->next;
 			}
 			if (!q) {
 				/* Make one then */
@@ -3225,8 +3184,7 @@
 					while(prev->next)
 						prev = prev->next;
 				}
-				var = ast_variable_browse(cfg, cat);
-				while(var) {
+				for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
 					if (!strcasecmp(var->name, "member")) {
 						/* Add a new member */
 						ast_copy_string(interface, var->value, sizeof(interface));
@@ -3250,7 +3208,6 @@
 					} else {
 						queue_set_param(q, var->name, var->value, var->lineno, 1);
 					}
-					var = var->next;
 				}
 				if (!new) 
 					ast_mutex_unlock(&q->lock);
@@ -3260,7 +3217,6 @@
 				}
 			}
 		}
-		cat = ast_category_browse(cfg, cat);
 	}
 	ast_config_destroy(cfg);
 	q = queues;
@@ -3396,11 +3352,9 @@
 	
 	ast_mutex_lock(&qlock);
 	for (q = queues; q; q = q->next) {
-		if (!strncasecmp(word, q->name, wordlen)) {
-			if (++which > state) {
-				ret = strdup(q->name);	
-				break;
-			}
+		if (!strncasecmp(word, q->name, wordlen) && ++which > state) {
+			ret = strdup(q->name);	
+			break;
 		}
 	}
 	ast_mutex_unlock(&qlock);
@@ -3532,9 +3486,8 @@
 
 	if (ast_strlen_zero(penalty_s))
 		penalty = 0;
-	else if (sscanf(penalty_s, "%d", &penalty) != 1) {
+	else if (sscanf(penalty_s, "%d", &penalty) != 1)
 		penalty = 0;
-	}
 
 	if (ast_strlen_zero(paused_s))
 		paused = 0;
@@ -3606,11 +3559,7 @@
 	if (set_member_paused(queuename, interface, paused))
 		astman_send_error(s, m, "Interface not found");
 	else
-		if (paused)
-			astman_send_ack(s, m, "Interface paused successfully");
-		else
-			astman_send_ack(s, m, "Interface unpaused successfully");
-
+		astman_send_ack(s, m, paused ? "Interface paused successfully" : "Interface unpaused successfully");
 	return 0;
 }
 
@@ -3665,24 +3614,14 @@
 {
 	/* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue>; 6 - penalty; 7 - <penalty> */
 	switch (pos) {
-	case 3:
-		/* Don't attempt to complete name of member (infinite possibilities) */
+	case 3:	/* Don't attempt to complete name of member (infinite possibilities) */
 		return NULL;
-	case 4:
-		if (state == 0) {
-			return strdup("to");
-		} else {
-			return NULL;
-		}
-	case 5:
-		/* No need to duplicate code */
+	case 4:	/* only one possible match, "to" */
+		return state == 0 ? strdup("to") : NULL;
+	case 5:	/* <queue> */
 		return complete_queue(line, word, pos, state);
-	case 6:
-		if (state == 0) {
-			return strdup("penalty");
-		} else {
-			return NULL;
-		}
+	case 6: /* only one possible match, "penalty" */
+		return state == 0 ? strdup("penalty") : NULL;
 	case 7:
 		if (state < 100) {	/* 0-99 */
 			char *num = malloc(3);
@@ -3735,23 +3674,16 @@
 	struct ast_call_queue *q;
 	struct member *m;
 
-	/* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - to; 5 - <queue> */
-	if ((pos > 5) || (pos < 3)) {
+	/* 0 - add; 1 - queue; 2 - member; 3 - <member>; 4 - from; 5 - <queue> */
+	if (pos > 5 || pos < 3)
 		return NULL;
-	}
-	if (pos == 4) {
-		if (state == 0) {
-			return strdup("from");
-		} else {
-			return NULL;
-		}
-	}
+	if (pos == 4)	/* only one possible match, 'from' */
+		return state == 0 ? strdup("from") : NULL;
 
-	if (pos == 5) {
-		/* No need to duplicate code */
+	if (pos == 5)	/* No need to duplicate code */
 		return complete_queue(line, word, pos, state);
-	}
 
+	/* here is the case for 3, <member> */
 	if (queues != NULL) {
 		for (q = queues ; q ; q = q->next) {
 			ast_mutex_lock(&q->lock);
