Added some exception handling for the db queries. Also added error codes when failures occur