Added javadoc, some refactoring