Handle empty lines and trailing newlines