Fix out of bounds error by flatmapping array down.

Fixes an out of bounds error during the copy process by replacing the copy with a flatMap which reduces a matrix into a single long array.