Move ref counting from macros to a class.