nested if statements to prevent undefined erros