Added javadoc and refoctored the code.