Quick rewrite of recursive method to minimise storage so can run (2^27)-1 ops without borking