structured code, add plot function