Add more safety to checks in indexOf loops