logic loop restructured