Check if s:history is not empty before accessing its last item
Fix calculation of selection size