Refactor the matching functions

A lot of renaming and un-duplicating for the 2 flavors for #6.