Refactored to clean up unused namespaces, optimize code.