Move variable setting into hook.

Not sure why this is needed but moving it outside had adverse effects.