rebuild with optional flatten.