OOP refactor.

Abstracted and refactored code to make it reusable.