refactor _flattened in cube and mask