added support for deeper flattening