added error handling for database communication