Fixed all the bugs of the new dynamic joint generation function