factor out 'tail' logic.