refactor and add recursive version
refactor and add more approaches
refactor
much neater
more neat