Mostly, I just separated out the code into a JS file

As the title, I pulled the code out into a .js file.

I can do a few more enhancements first chance I get, but this should resolve the scoping error Dan was having.