Refactoring + dcoumentation for tail call optimization