some half-baked refactoring for the plot function