Test and Refactoring

I begin to write test and refactor the code. NOT FINISH