Remove trailing spaces and extra newlines.