Reference and Optimized versions

Two versions: reference and optimized