You could avoid these variable gymnastics by pulling this out into a function.