MAINT: move array creation earlier and use goto for deallocs