corrected bug when length was greater than total