Fixed a bug with incrorrect name resolution.

-- I want to understand why an additional iteration is necessary, but at the moment I'm not sure.