fix bugs in pattern matching