Removing a lot of globals and further separation

It's far far far from nice shape, but it's getting closer. All circular dependencies have been removes and every file is capable of being AMDed now (almost all are).