Lots of refactoring and namespacing