fix indexing to be non-blocking. refactor and cleanup.