fixed memory leaks and errors
fixed memory leaks and errors