Refactored into a class

Refactored code into a class to avoid globals