fixed tail recursion optimization (manually)