Variable scope fixes

(see commit history)