Dedup logic in categories array (which currently doesn't get used).
Also bugfixes.