fix incorrect length and ancestor calculation.
format code.