Refactor and add a test code