Move variables to where they are used and give them better names. No functional change intended.