added cost function checking