Some bug fixes in the database