refactor resolve-scope into its own pass