Added tests for model and refactor model code to be more testable.