restructure scope control code
Merge Zach's scope control code changes.