optimize gallery + modal logic and markup