optimized to scan list of pairs of gls only once

self-explanatory