adjusted cost function
further optimized