optimized th.cyc function