better(?) flattening procedure