Eliminates globals in calc_cost method