Refactoring and added some javadoc.