Merge pull request #1377 from clebertsuconic/2.3.x

back porting some fixes from master
Only copy what is needed
Use duplicate