refactors script into a few functions with a main