support for gallery. tried to reduce code redundancy.