Implementing tail-call optimization