Adapting code to MVC pattern