[WIP] Fix ref counting

targets #479