Scope index variables properly