More tests and refactoring.

Re: #17