Complete Cost function