Refactored a lot to make it more object oriented