improved indexAt method