Fix bugs in tail call optimization