Make myFlatten more memory efficient