added optimize function