moved where it calculates cost so it stops when completed