Fix awful reference-tracking bugs.