update algorithm in recursive helper function