Updated the code to include module and encapsulation of the code into iffy to remove global vars