move the code to model

- added tests