refactor and added tests. (#221)