Corrected frequency calculation.
Improved Gradient.
Removed unnecèssary if.