Refactored into recursive program structure