Implement setNamespace and fix some namespace handling issues