Add Javadoc and minor refactoring