added dynamic gCost function