fix critical bug in recursive copy function