refactor bash scripts into functions

add some comments