simplified the code dramatially by mapping over a sequence of data and a sequence of index values