Improve and simplify matching logic.

Closes #9