Condensed code down to live inside the anonymous jquery wrapper, so it doesn't pollute the global namespace with internal function names.