Updated cost function + fixed imports