diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index 02902bc8a59df76507b2356e9acd23c445b97267..409146c4a3e7a20b28a218280d172bd21390c0e3 100644
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -3541,9 +3541,10 @@ void LLAppViewer::idle()
 	    // NOTE: Starting at this point, we may still have pointers to "dead" objects
 	    // floating throughout the various object lists.
 	    //
+		idleNameCache();
     
 		idleNetwork();
-	    	        
+
 
 		// Check for away from keyboard, kick idle agents.
 		idle_afk_check();
@@ -3878,6 +3879,41 @@ void LLAppViewer::sendLogoutRequest()
 	}
 }
 
+void LLAppViewer::idleNameCache()
+{
+	// Neither old nor new name cache can function before agent has a region
+	LLViewerRegion* region = gAgent.getRegion();
+	if (!region) return;
+
+	// deal with any queued name requests and replies.
+	gCacheName->processPending();
+
+	// Agent may have moved to a different region, so need to update cap URL
+	// for name lookups.  Can't do this in the cap grant code, as caps are
+	// granted to neighbor regions before the main agent gets there.  Can't
+	// do it in the move-into-region code because cap not guaranteed to be
+	// granted yet, for example on teleport.
+	std::string name_lookup_url;
+	name_lookup_url.reserve(128); // avoid a memory allocation below
+	name_lookup_url = region->getCapability("GetDisplayNames");
+
+	// Ensure capability has been granted
+	U32 url_size = name_lookup_url.size();
+	if (url_size > 0)
+	{
+		// capabilities require URLs with slashes before query params:
+		// https://<host>:<port>/cap/<uuid>/?ids=<blah>
+		// but the caps are granted like:
+		// https://<host>:<port>/cap/<uuid>
+		if (name_lookup_url[url_size-1] != '/')
+		{
+			name_lookup_url += '/';
+		}
+		LLAvatarNameCache::setNameLookupURL(name_lookup_url);
+	}
+	LLAvatarNameCache::idle();
+}
+
 //
 // Handle messages, and all message related stuff
 //
@@ -3905,10 +3941,6 @@ void LLAppViewer::idleNetwork()
 	{
 		LLFastTimer t(FTM_IDLE_NETWORK); // decode
 		
-		// deal with any queued name requests and replies.
-		gCacheName->processPending();
-		LLAvatarNameCache::idle();
-
 		llpushcallstacks ;
 		LLTimer check_message_timer;
 		//  Read all available packets from network 
diff --git a/indra/newview/llappviewer.h b/indra/newview/llappviewer.h
index a915b7fa50c8da0dc88223b4b9d4ed5fce9cc1a8..ff3816922e3c3d0fc2ec346447b5e4e2c9032f83 100644
--- a/indra/newview/llappviewer.h
+++ b/indra/newview/llappviewer.h
@@ -210,6 +210,8 @@ class LLAppViewer : public LLApp
     
     void idle(); 
     void idleShutdown();
+	// update avatar SLID and display name caches
+	void idleNameCache();
     void idleNetwork();
 
     void sendLogoutRequest();
diff --git a/indra/newview/llviewerregion.cpp b/indra/newview/llviewerregion.cpp
index feade68edc5644505cc3d4962f6d521956939e12..83c5760b5899a6d452b1adf133985a44685b005c 100644
--- a/indra/newview/llviewerregion.cpp
+++ b/indra/newview/llviewerregion.cpp
@@ -175,24 +175,6 @@ class BaseCapabilitiesComplete : public LLHTTPClient::Responder
 				mRegion->showReleaseNotes();
 			}
 		}
-
-		// JAMESDEBUG *TODO* THIS IS WRONG!!!!!!!!!!!!!!!!!!!!!
-		// this isn't necessarily the region the viewer is in
-
-		// Avatar name lookup library needs to know who to ask
-		std::string name_lookup_url = mRegion->getCapability("GetDisplayNames");
-		// capabilities require URLs with slashes before query params, like:
-		// https://<host>:<port>/cap/<uuid>/?ids=<blah>
-		// but the caps are granted like:
-		// https://<host>:<port>/cap/<uuid>
-		U32 url_size = name_lookup_url.size();
-		if (url_size > 0 && name_lookup_url[url_size-1] != '/')
-		{
-			name_lookup_url += '/';
-		}
-		LLAvatarNameCache::setNameLookupURL(name_lookup_url);
-
-		llinfos << "JAMESDEBUG cap " << name_lookup_url << llendl;
 		
 		if (STATE_SEED_GRANTED_WAIT == LLStartUp::getStartupState())
 		{