Refactoring into models, lots of code cleanup