Change order of declarations, and make corresponding frontend function
tail recursive.