Update dblayer with rewritten and tested code. Fix code. Fix tests.