Refactored to use OO instead of spaghetti code