refactored code to make it more OO