I solved my issue in another way. Realized that I didn't really need to create the array and all conditions on each new bar, so I now use a switch case statement instead which is a lot more efficient. And it has the benefit of not crashing ;-)