function dwFindPortraitMatch()
	if dwAvoidRecursion then return end
	local dwPortraitNames={}
	local nmax=Infinity_GetNumCharacters() - 1
	-- count number of characters currently loaded
	local count=0
	for k,v in pairs (characters) do
		count=count + 1
	end
	if (count<nmax) then
		dwAvoidRecursion=true
		e:GetActiveEngine():OnLeftPanelButtonClick(4)
		for n=0,nmax do
			print('clicking portrait '..n)
			Infinity_OnPortraitLClick(n)
		end
		e:GetActiveEngine():OnLeftPanelButtonClick(0)
		Infinity_OnPortraitLClick(0)
		dwAvoidRecursion=false
	end
	
	local n=nil
	-- get names associated with portraits
	for n=0,nmax do
		str=Infinity_GetPortraitTooltip(n)
			if str then
			_,_,dwPortraitNames[n]=string.find(str,"([^\n]+)")
		end
	end
	-- get names associated with current IDs and match
	local dwPortraitLookup={}
	local dwPortraitLookupBackwards={}
	for id,char in pairs (characters) do
		for n=0,nmax do
			if (dwPortraitNames[n]==char.name) then
				dwPortraitLookup[id]=n
				dwPortraitLookupBackwards[n]=id
				print('Matched '..id..' with '..n)
			end
		end

	end
	return dwPortraitLookup,dwPortraitLookupBackwards
end


