move the opt. loop in a separate function
.
optimize works directly on a callable