Fix database errors

and some other reported  bugs