fix bugs and add cost output