Replaced repeated calls to $() to look up common elements with cached copies that are looked up just once.