fix indexing+scoping bug