more generic minimization function