Updated function to strip trailing whitespace