improved flatten nodes