Added javadocs, refactored some codes