refactored scripts with more closure