refactored nested if statements to cond statement