rewrote pattern matching section