Fixed memory leak and incorrect copy bugs.

 - The result of a call to realloc was erroneously thrown away, creating a potential memory leak.
 - Values were being copied to an array on the stack, then memcpy'ed into an array on the heap. Since the heap array is freshly allocated, it should be completely safe to memcpy the data directly into it.
 - The inner loop was copying the same value several times, rather than copying several values in series.