re-organized DOM reads and writes to prevent DOM thrashing

(minor performance improvement)