Refactoring to proper mvc pattern.