Refactor the if/else flow