Restructured the code, fixed a minor model bug