change if to pattern matching