Fix the previous version by encapsulating the head reference into a class.