refactored mode function to pattern match instead of using if/else