working version with powell optim