refractoring of plotting code