Refactor and add some test code
Refactor and add test codes