changed a function into a nested function