Made Into Function_ Moved Other Functions

Moved all functions outside (to beginning) and made full script into a
function that can be run from the command line (rather than just
pasting in the entire script which is what I did before)