TestLine returns contents values, not leaf numbers.
MakeTnode stores the leaf's contents into the tnode's parent, so you'll need to either modify the tnodes or just directly use the dnodes+dplanes
that's why your code deals with bits instead of using the if(node<0)leafnum=(-1-node); conversion that is native to the bsp format.