Added some smart preprocessing of the imag