improved threshold trimming