Fix TypeError when iterating over variable that can be 'None'