Updated implementation, separated out per-scope functionality into separate Scope class