made sure input buffer is null terminated
made sure thread finished before freeing memory