Multiple fixes to `set` and scopes

A number of fixes to the environment and to the `set` builtin, largely around better handling of scopes.