Minor optimizations

Reduce some unnecessary calls and shuffle code around.