Replaced foreach with a blocking loop