[CS] Organize code into namespaces

Fixes bugs in db fns as well.