better stacked gallery logic