I think this foreach cycle should be nested inside upper if condition, right?