Refactored nasty global stuff into a class