Major refactoring, MVC support

Refactored code (still in progress) to use the MVC pattern.