Refactor casting of arrays

Refactor how the casting of arrays is done.