fixed memory leaks and buffer overruns