implements pattern matching