Add test coverage

Test library and CLI code. Some internal code refactoring.