Use new[]/delete[] and fix memleak