Several fixes to db structure