Fixed a potential buffer overflow, and two memory leaks.