Merged changes from global-optimisation branch.