add better flattening