Fix memory leak with smart pointers, fix initialization pattern for reference count