Added a test, and some refactor.