fix variable scope issues