Implemented minimizing function