I'd move the "term" loop as the first loop, so that we can break the two inner loops when we found the term. (and we can remove the nested if block)