fix foreach logic