Fix some namespace issues, whole template refactoring