Refactor to use OOP and beautify a bit.