foreach replaced by for to improve performance