fix a lot of bugs in db