Fixed a few scoping and typing issues