refactored script into functions