Added unit test, refactored algo in model