Implemented database update logic, fixed some bugs.