better error checking for TrimEnds