Conditional nested in a for loop