Added javadoc and did small refactoring.