Variable update.

Apparently camelcase is cool in perl, but python players like the
underscore.
Replaced spaghetti with a function.

Turns out this whole thing could be simplified down into one function so
I tidied it up as best as I could.