replacing if with pattern matching