Refactoring and add javadoc.