Replace if statements with pattern matching