Rewrite of the tempral function code from cepl.