added explanation comments to legend building code