Better optimisation

Gave up on the silly hillclimbing thing, started trying the built-in
optimisation methods. Still encountering some problems.