change from iterative loop to recursive