Added optimize function.