added majority of the script to def main