fix incorrect code (invalid assumptions on the result of index_of)
simpler code