small bug fixes and error handling for database operations