fix test
fix deriv in dfun template
add unpack state