...
    freq = {}
    for w in words:
        wl = w.lower()
        if wl in STOPWORDS:
            continue
        freq[wl] = freq.get(wl, 0) + 1

    for wl, _ in sorted(freq.items(), key=lambda kv: kv[1], reverse=True):
        if len(tags) >= max_tags:
            break
        if wl in STOPWORDS:
            continue
        if wl not in [x.lower() for x in tags]:
            tags.append(wl)

  ...