add legend and some other codes