correct bug
implemented
implemented
added output value
More precision in calculating stopping point
clearvars better than clear all in this case
Defintely (hope) corrected.

Should work in all cases now.