Improved plotting function