improved pattern matching