Improve how versions are compared.

Previously version numbers were split into major and minor numbers, then
compared individually, resulting awkward version check conditions.  This is
now improved so that version numbers are converted into zero-padded strings.
They can then be compared without any logical gymnastics.

Also convert some variables in functions into local variables, and reduce
usage of global-scope variables from within functions.