fix some bugs in deriv gen