Fixed some bugs in DB, added tests