Move tripgen to header file; fix memory leak