Optimize the code with memcpy