Most current working code.

This file is now structured properly. Some of this stuff will move to other files because the main file is where stuff is run, not defined.