Small refactor to avoid global scope, use promises, minor cleanup.

This puts all the code in an IIFE (closure) to avoid everything being in the global scope.  It also uses `this` instead of `self`, except where it was necessary to capture `self`.  It uses the jQuery deferred (promise-like) API instead of the older callback style ajax handling.