Improve and simplify matching logic.

Closes #9
Merge pull request #18 from eapache/improve-query

Improve and simplify matching logic.