Improved removal of trailing whitespace