Changed db to use more shared_ptrs and some judging refactoring.