Do less work when pattern matching. Fix nil pattern.
Added a unit test for pattern matching.