Complete rewrite with recursion