Optimise version comparisons

This is a partial merge of #482. It includes the version comparison
code with some further simplifications and a bugfix, but not the
GUI optimisations.