Factor out iteration in anticipation of looking up substrings