more moving of code to functions
reformatting and more comments
add a space