Fix exponential running time in expression translator

It now runs in linear time