diff --git a/pbx.c b/pbx.c
index 00d53e01e1cde030d0f802c95c4031c68538b59b..0d3ecf92d3af5e2aefdfe997bc2c8d0c6b3be14a 100755
--- a/pbx.c
+++ b/pbx.c
@@ -738,8 +738,9 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 	int offset,offset2;
 	struct ast_var_t *variables;
 	char *name, *num; /* for callerid name + num variables */
-	struct varshead *headp;
-        headp=&c->varshead;
+	struct varshead *headp=NULL;
+    if (c) 
+		headp=&c->varshead;
     *ret=NULL;
         /* Now we have the variable name on cp3 */
 	if ((first=strchr(var,':'))) {
@@ -773,7 +774,7 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 		else
 			*ret+=strlen(*ret)+offset;
 		(*ret)[offset2] = '\0';
-	} else if (!strcmp(var, "CALLERIDNUM")) {
+	} else if (c && !strcmp(var, "CALLERIDNUM")) {
 		if (c->callerid)
 			strncpy(workspace, c->callerid, workspacelen - 1);
 		ast_callerid_parse(workspace, &name, &num);
@@ -782,7 +783,7 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 			*ret = num;
 		} else
 			*ret = workspace;
-	} else if (!strcmp(var, "CALLERIDNAME")) {
+	} else if (c && !strcmp(var, "CALLERIDNAME")) {
 		if (c->callerid)
 			strncpy(workspace, c->callerid, workspacelen - 1);
 		ast_callerid_parse(workspace, &name, &num);
@@ -790,21 +791,21 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 			*ret = name;
 		else
 			*ret = workspace;
-	} else if (!strcmp(var, "CALLERID")) {
+	} else if (c && !strcmp(var, "CALLERID")) {
 		if (c->callerid) {
 			strncpy(workspace, c->callerid, workspacelen - 1);
 			*ret = workspace;
 		} else 
 			*ret = NULL;
-	} else if (!strcmp(var, "HINT")) {
+	} else if (c && !strcmp(var, "HINT")) {
 		if (!ast_get_hint(workspace, workspacelen - 1, c, c->context, c->exten))
 			*ret = NULL;
 		else
 			*ret = workspace;
-	} else if (!strcmp(var, "EXTEN")) {
+	} else if (c && !strcmp(var, "EXTEN")) {
 		strncpy(workspace, c->exten, workspacelen - 1);
 		*ret = workspace;
-	} else if (!strncmp(var, "EXTEN-", strlen("EXTEN-")) && 
+	} else if (c && !strncmp(var, "EXTEN-", strlen("EXTEN-")) && 
 		/* XXX Remove me eventually */
 		(sscanf(var + strlen("EXTEN-"), "%d", &offset) == 1)) {
 		if (offset < 0)
@@ -814,25 +815,25 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 		strncpy(workspace, c->exten + offset, workspacelen - 1);
 		*ret = workspace;
 		ast_log(LOG_WARNING, "The use of 'EXTEN-foo' has been derprecated in favor of 'EXTEN:foo'\n");
-	} else if (!strcmp(var, "RDNIS")) {
+	} else if (c && !strcmp(var, "RDNIS")) {
 		if (c->rdnis) {
 			strncpy(workspace, c->rdnis, workspacelen - 1);
 			*ret = workspace;
 		} else
 			*ret = NULL;
-	} else if (!strcmp(var, "CONTEXT")) {
+	} else if (c && !strcmp(var, "CONTEXT")) {
 		strncpy(workspace, c->context, workspacelen - 1);
 		*ret = workspace;
-	} else if (!strcmp(var, "PRIORITY")) {
+	} else if (c && !strcmp(var, "PRIORITY")) {
 		snprintf(workspace, workspacelen, "%d", c->priority);
 		*ret = workspace;
-	} else if (!strcmp(var, "CHANNEL")) {
+	} else if (c && !strcmp(var, "CHANNEL")) {
 		strncpy(workspace, c->name, workspacelen - 1);
 		*ret = workspace;
-	} else if (!strcmp(var, "EPOCH")) {
+	} else if (c && !strcmp(var, "EPOCH")) {
 	  snprintf(workspace, workspacelen -1, "%u",(int)time(NULL));
 	  *ret = workspace;
-	} else if (!strcmp(var, "DATETIME")) {
+	} else if (c && !strcmp(var, "DATETIME")) {
 	  thistime=time(NULL);
 	  localtime_r(&thistime, &brokentime);
 	  snprintf(workspace, workspacelen -1, "%02d%02d%04d-%02d:%02d:%02d",
@@ -844,18 +845,20 @@ static void pbx_substitute_variables_temp(struct ast_channel *c,const char *var,
 		   brokentime.tm_sec
 		   );
 	  *ret = workspace;
-	} else if (!strcmp(var, "UNIQUEID")) {
+	} else if (c && !strcmp(var, "UNIQUEID")) {
 	  snprintf(workspace, workspacelen -1, "%s", c->uniqueid);
 	} else {
-		AST_LIST_TRAVERSE(headp,variables,entries) {
+		if (c) {
+			AST_LIST_TRAVERSE(headp,variables,entries) {
 #if 0
-			ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
+				ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
 #endif
-			if (strcasecmp(ast_var_name(variables),var)==0) {
-				*ret=ast_var_value(variables);
-				if (*ret) {
-					strncpy(workspace, *ret, workspacelen - 1);
-					*ret = workspace;
+				if (strcasecmp(ast_var_name(variables),var)==0) {
+					*ret=ast_var_value(variables);
+					if (*ret) {
+						strncpy(workspace, *ret, workspacelen - 1);
+						*ret = workspace;
+					}
 				}
 			}
 		}
diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c
index 8443863540e8b93a3e001048247d75f336ae961c..eb73c5b298be437e7d38f65337d70b3c89c52d17 100755
--- a/pbx/pbx_config.c
+++ b/pbx/pbx_config.c
@@ -1538,6 +1538,7 @@ static int pbx_load_module(void)
 	char *cxt, *ext, *pri, *appl, *data, *tc, *cidmatch;
 	struct ast_context *con;
 	char *start, *end;
+	char realvalue[256] = "";
 
 	cfg = ast_load(config);
 	if (cfg) {
@@ -1548,7 +1549,8 @@ static int pbx_load_module(void)
 			"writeprotect"));
 		v = ast_variable_browse(cfg, "globals");
 		while(v) {
-			pbx_builtin_setvar_helper(NULL, v->name, v->value);
+			pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
+			pbx_builtin_setvar_helper(NULL, v->name, realvalue);
 			v = v->next;
 		}
 		cxt = ast_category_browse(cfg, NULL);