diff --git a/src/anchor.c b/src/anchor.c
index 006e5da4..a374ab78 100644
--- a/src/anchor.c
+++ b/src/anchor.c
@@ -1517,6 +1517,7 @@ void _getdns_start_fetching_ta(getdns_context *context, getdns_eventloop *loop)
 	char tas_hostname[256];
 	const char *verify_CA;
 	const char *verify_email;
+	getdns_context *sys_ctxt;
 
 	if ((r = _getdns_get_tas_url_hostname(context, tas_hostname, NULL))) {
 		DEBUG_ANCHOR("ERROR %s(): Could not get_tas_url_hostname"
@@ -1557,32 +1558,7 @@ void _getdns_start_fetching_ta(getdns_context *context, getdns_eventloop *loop)
 	DEBUG_ANCHOR("%s on the %ssynchronous loop\n", __FUNC__,
 	             loop == &context->sync_eventloop.loop ? "" : "a");
 
-	while (!context->sys_ctxt) { /* Used as breakable if. Never repeats. */
-		if ((r = getdns_context_create_with_extended_memory_functions(
-		    &context->sys_ctxt, 1, context->mf.mf_arg,
-		    context->mf.mf.ext.malloc, context->mf.mf.ext.realloc,
-		    context->mf.mf.ext.free)))
-			DEBUG_ANCHOR("Could not create system context: %s\n"
-			            , getdns_get_errorstr_by_id(r));
-
-		else if ((r = getdns_context_set_eventloop(
-		    context->sys_ctxt, loop)))
-			DEBUG_ANCHOR("Could not configure %ssynchronous loop "
-			             "with system context: %s\n"
-			            , ( loop == &context->sync_eventloop.loop
-			              ? "" : "a" )
-			            , getdns_get_errorstr_by_id(r));
-
-		else if ((r = getdns_context_set_resolution_type(
-		    context->sys_ctxt, GETDNS_RESOLUTION_STUB)))
-			DEBUG_ANCHOR("Could not configure system context for "
-			             "stub resolver: %s\n"
-			            , getdns_get_errorstr_by_id(r));
-		else
-			break;
-
-		getdns_context_destroy(context->sys_ctxt);
-		context->sys_ctxt = NULL;
+	if (!(sys_ctxt = _getdns_context_get_sys_ctxt(context, loop))) {
 		DEBUG_ANCHOR("Fatal error fetching trust anchor: "
 		             "missing system context\n");
 		context->trust_anchors_source = GETDNS_TASRC_FAILED;
@@ -1592,7 +1568,7 @@ void _getdns_start_fetching_ta(getdns_context *context, getdns_eventloop *loop)
 	scheduled = 0;
 #if 1
 	context->a.state = TAS_LOOKUP_ADDRESSES;
-	if ((r = _getdns_general_loop(context->sys_ctxt, loop,
+	if ((r = _getdns_general_loop(sys_ctxt, loop,
 	    tas_hostname, GETDNS_RRTYPE_A, NULL, context,
 	    &context->a.req, NULL, _tas_hostname_lookup_cb))) {
 		DEBUG_ANCHOR("Error scheduling A lookup for %s: %s\n"
@@ -1603,7 +1579,7 @@ void _getdns_start_fetching_ta(getdns_context *context, getdns_eventloop *loop)
 
 #if 1
 	context->aaaa.state = TAS_LOOKUP_ADDRESSES;
-	if ((r = _getdns_general_loop(context->sys_ctxt, loop,
+	if ((r = _getdns_general_loop(sys_ctxt, loop,
 	    tas_hostname, GETDNS_RRTYPE_AAAA, NULL, context,
 	    &context->aaaa.req, NULL, _tas_hostname_lookup_cb))) {
 		DEBUG_ANCHOR("Error scheduling AAAA lookup for %s: %s\n"
diff --git a/src/context.c b/src/context.c
index e12f4c04..1fc188ff 100644
--- a/src/context.c
+++ b/src/context.c
@@ -5048,5 +5048,40 @@ getdns_context_set_appdata_dir(
 	return GETDNS_RETURN_GOOD;
 }
 
+getdns_context *_getdns_context_get_sys_ctxt(
+    getdns_context *context, getdns_eventloop *loop)
+{
+	getdns_return_t r;
+
+	if (context->sys_ctxt)
+		return context->sys_ctxt;
+
+	if ((r = getdns_context_create_with_extended_memory_functions(
+	    &context->sys_ctxt, 1, context->mf.mf_arg,
+	    context->mf.mf.ext.malloc, context->mf.mf.ext.realloc,
+	    context->mf.mf.ext.free)))
+		DEBUG_ANCHOR("Could not create system context: %s\n"
+			    , getdns_get_errorstr_by_id(r));
+
+	else if ((r = getdns_context_set_eventloop(
+	    context->sys_ctxt, loop)))
+		DEBUG_ANCHOR("Could not configure %ssynchronous loop "
+			     "with system context: %s\n"
+			    , ( loop == &context->sync_eventloop.loop
+			      ? "" : "a" )
+			    , getdns_get_errorstr_by_id(r));
+
+	else if ((r = getdns_context_set_resolution_type(
+	    context->sys_ctxt, GETDNS_RESOLUTION_STUB)))
+		DEBUG_ANCHOR("Could not configure system context for "
+			     "stub resolver: %s\n"
+			    , getdns_get_errorstr_by_id(r));
+	else
+		return context->sys_ctxt;
+
+	getdns_context_destroy(context->sys_ctxt);
+	context->sys_ctxt = NULL;
+	return NULL;
+}
 
 /* context.c */
diff --git a/src/context.h b/src/context.h
index 54a46306..5fc0ff38 100644
--- a/src/context.h
+++ b/src/context.h
@@ -554,4 +554,7 @@ int _getdns_context_write_priv_file(getdns_context *context,
 
 int _getdns_context_can_write_appdata(getdns_context *context);
 
+getdns_context *_getdns_context_get_sys_ctxt(
+    getdns_context *context, getdns_eventloop *loop);
+
 #endif /* _GETDNS_CONTEXT_H_ */