refactored the code to be more OO