improved memory management and leaks