improved logic for choosing which lines to print.