Refactor json stuff; reorganize db module; fix tests