changed to unsigned long for length variables