Refactored the code, made unit test pass.