replace foreach with count