Added more javadoc and increased test coverage. Minor refactoring.