Fix buffer overflow
Casts are stronger than necessary
Fix uninitialized member vars
Fix mismatched free

Memory was allocated with new on line 304
Fix leaked member var
Fix potential buffer overflows