Fixed reference counting and memory leak