restructure loop