Some javadoc, comments and minor refactoring