Refactored code to avoid usage of else