Minor cleanup and an off-by-1 error fix.
Cleanup of all the 'global' statements that weren't needed.