made length tail recursive