added @tailrec annotation