Update to fix retry loop bug, currently fully function as far as I can see, refactor to remove cruft