Fixed numerous scope issues.