function and script refactoring