Combine the if and for loops