BUG 12027, Follow up: Refactoring

- Global variables are BAD!
- Minor code cleanup in places.
BUG 8446, followup: Initial Working Tests