Final implementation (fixed a bug with smoothing)