Optim on magnet algorithm after review with @mourner