improved error handling and database connection usage