JSHint Fixes

Our JSHint setup was not catching bugs (see the recent bug on `sync` being undefined).  This makes it more robust, and fixes the things it found.  Mostly it's removing unused variables, moving things around to be defined before first use, etc.