Refactor casting of arrays