Remove white-space at end of line and add a terminating newline