Complete namespace refactoring and improve thread safety