cleanup and fixing mem leaks