Add some clean up code to ref