nit: could cache the head element outside of this function