Main part of optimisation routine moved to separate file.