Fixed up min_cost function a bit, still not working properly...