Shouldn't casting to array be done before iterating?