version working for smooth functions