Fix issue #1104 - regression issue with %%name%% variable

Moved the setting up of non-ID dependent variable replacements to more logical places.

My fingers are itching to rewrite this whole function as it seems to be causing more and more issues. Will do when I have the time, which unfortunately probably won't be all that soon.