Code quality changes based on JM's checking of Elin's branch +

discussion of scope.  Also significant help from Nikolai on this.