refactoring
Recursive function added, improvements.