Pushed some debugging things, couldn't really see anything wrong with algorithm, but it seems to work for non decreasing functions that are extremely jumpy