Added database error checking