Reworked pattern matching.
Fixes #7.