fixed a bug 
rewrote finding the last value which can be plotted to the right or left