diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 0e28fd16348284ec11b20b8d733df3df04feb570..0aa8f57ec302543ea6c78f059ef19a281d0ac016 100755
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -4538,8 +4538,17 @@ static int get_refer_info(struct sip_pvt *p, struct sip_request *oreq)
 		p2 = iflist;
 		while(p2) {
 			if (!strcmp(p2->callid, tmp5)) {
-				/* Go ahead and lock it before returning */
+				/* Go ahead and lock it (and its owner) before returning */
 				ast_mutex_lock(&p2->lock);
+				if (p2->owner) {
+					while(ast_mutex_trylock(&p2->owner->lock)) {
+						ast_mutex_unlock(&p2->lock);
+						usleep(1);
+						ast_mutex_lock(&p2->lock);
+						if (!p2->owner)
+							break;
+					}
+				}
 				p->refer_call = p2;
 				break;
 			}
@@ -6471,6 +6480,8 @@ static int handle_request(struct sip_pvt *p, struct sip_request *req, struct soc
 				if (p->refer_call) {
 					ast_log(LOG_DEBUG,"202 Accepted (supervised)\n");
 					attempt_transfer(p, p->refer_call);
+					if (p->refer_call->owner)
+						ast_mutex_unlock(&p->refer_call->owner->lock);
 					ast_mutex_unlock(&p->refer_call->lock);
 					p->refer_call = NULL;
 					p->gotrefer = 1;
diff --git a/config.c b/config.c
index 574561ab3cc6af6fb3bcd1e43a27b1d323120929..954d0950928c9f7a511e654a0c650210102a7370 100755
--- a/config.c
+++ b/config.c
@@ -740,9 +740,8 @@ static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, s
 			return NULL;
 		}
 		while(!feof(f)) {
-			fgets(buf, sizeof(buf), f);
 			lineno++;
-			if (!feof(f)) {
+			if (fgets(buf, sizeof(buf), f)) {
 				if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
 #ifdef PRESERVE_COMMENTS
 				, acs