Generalise to any function (unfinished)