Use fewer loops for large messages
Properly allocate buffer