Model code refactorization (unstable, untested, wip)