updated memory management of 2D & 3D arrays