Improved smoothing function