Refactor namespaces. Add more tests and improve templating