Optimized Reference Implementation

Optimized the TH implementation with 10% faster performance over previous versions.