Ported recursive version (Robert Glueck's version)