Corrected cost function calculation.