code rearrange, adjusted comments