fixed a few invalid reads
fixed memory leaks