Fix some pattern matching bugs