Refactored some code and added an OO version