added automatic tool length detection