some refactoring. nested function definitions don't work.