Fixed memory leak by using delete[] for allocated arrays