Cleaned up the script and fixed bugs.

Just need to fix the bug with JSON being undefined and any other bugs
that have yet to crop up.