trim: Mark processed state-pairs as seen to avoid looping

Also, more intermediate vars for more readability than it->first etc