Factored out the cost functions into separate classes