Refactor tests and index_by method