fixed error handling and database call