refactoring code for function estimation