Adding cost function
Adding gradient