scavanged for code to get lengths to work