Flatten logic