Added more tests for coverage and a number of refactoring.