Reduce some variable scope
Merge nested if statements
Cache some accessor results

This may be overkill, but I don't think it's *theoretically* overkill;
we call non-inline functions in between these accessor calls and so
even though we can be sure the results haven't changed, the compiler
probably can't.
Only assert scalars are scalars in dbg mode