model refactoring and some minor fixes