refactor nested if with guard clause