Reorganize the plot code