tidy up flattening transformation