changed some memcpy calls to memmove since memcpy doesn't allow the buffers to overlap, but it's probably harmless.