Fixed trimmed length calculation, and removed redundant duplicate code.