Refactored if-elsif block to more readable case-when block