Refactoring nested if-elses