Refactor plot code.