If the first grep is already quiet, the second one will not find anything and exit with error code 1. So the quiet flag has to be on the second grep command to get the script working.
Previous version could not work as grep -q does not print anything to stdout, only exits with a status code depending on a match was found or not. Hence the variables were always empty and could not be compared with 0