Add more test and refactor code