Refactor

  * record lengths so we're not getting them twice
  * use memcpy rather than strncat
  * fix leaks