Refactor into MVC pattern