Fix out of bounds error by flatmapping array down.
fix out the out of bound bug by Frank