Refactor key expansion and namespacing