Fix ref-count problems