Refactor nested if/else