refactoring nestFunction