rewrote find_link_lengths to try and reduce ridiculous memory usage- trying to close and reopen file and explicitly delete everything in the stack, which should not be necessary...