Fixing issue with length not being calculated correctly. Output was bring
trimmed after truncating/cleaning, resulting in a (sometimes) much shorter
string than requested. Modified to trim the input before truncation.