Scoping

Fixes some scoping issues that can arise from using functions.