refactored if-else structure to pattern matching