fixed some bugs that could lead to memory leaks