Remove extraneous element queries

Improve performance slightly by removing additional element queries, and improve the readability of some of the variable blocks.