Overhaul of gallery logic