flattening refactor