Moved some globals to main function. Added comments