cost function implemented and tested