Simplify the logic in TMatchState.

I'll only merge this one after rebasing on top of the formatting changes, once they are submitted. The actual logic changes are in the last commit.