Fixed bugs introduced by cancelling glogals.
Refactoring chiefly based on JSLint.