refactored to have better MVC principles