added recursive implementation