fixed memory leaks. added new function