improved plot function