In the CHMT function the curly braces were missing after the first if condition. As a result, the function was performing the computation for all the variables.