Fixed variable scope issues #2