Fixing cost function