Fully working version using only recursion and continuation-passing style.