Improved readability by replacing nested if else with pattern matching