Full working version. Untested for the harmonics calculations.