Added exception handling for when queries fail