Removing an if in place of pattern matching