added code to plot and calculate w