First running version of optimFunc