fix reference count problems (memory leaks)