improve line_text, remove_last_line and clean_document