improved pattern matcher