Cleaned up various issues with scoping