Replaced if else with try Except to avoid unnecessary if checks.