Move scopes code to a shared file