Fixed passel of small errors, mostly inadvertent globals due to missing "var" keyword