Refactoring and javadoc added.