Refactored and made more object oriented