Rewrite most ofthe pattern matcher
Add pattern matching tests