tailrec solution.