Further refactoring and namespace fixes.