Refactored, with MVC pattern