add tail recursive solution