tidy up and add documentation for _filter_line