Made some private functions inner functions

Added @tailrec annotations.