Improve namespace matching code