added recursive version of the algo