Use indexOf instead of a loop