Declare more variables as local

Might have some conflicts with https://github.com/creationix/nvm/pull/358 and https://github.com/creationix/nvm/pull/360 among others.