cleanup and add comments to output code