Optimize complex DOM query