Major refactor. Fix issue #5. Fix issue #3.