works but crashes when array is larger than around 1050000 elements