Changed the cost function

Renamed some some variables and cleaned some functions