Changed lots of variables and functions from scope variables to normal JavaScript variables