flatten and prune