Various bugfixes

This commit fixes all the places where the PyDev plugin for Eclipse
could find mistakes, e.g. "undefined variable" errors. Usually they were
the cause of previous refactoring: such-and-such a function had been
renamed, or moved into class XYZ, but one or two cases of calling that
function hadn't been updated. Those should be fixed now.
A few typo and whitespaces fixes
Improve loop by using enumerate()

This is the kind of situation enumerate() was written for; let's use it.