Makes get_substitutions tail-recursive