The nested if block here looks like you could break out into a recursive function for the scenario stuff.