completed Cost Function