Refactored to be more MVC oriented