migrate flatten complete and fix a test