Changed 'tail[0].nil?' to use 'tail.empty?'
Changed 'tail[1].nil?' to use 'tail.size == 1' for readability.