Refactored the code with the switch statement.