Major rewrite

Hey, I rewrote a lot of the script in a way that I think is better, but I could be dangerously wrong so just go through the changes and comment on anything that don't make-a no sense. Also, don't be afraid to bring @nifty in on this to judge my poor JavaScript skillzzz.