Fix various scoping issues.

Basically just add var all over the place.