reverted moving code into def main(), problems with global variables, will have to be fixed later