A bit of refactoring with getting rid of some GLOBALS.