Added Javadoc and some comments. Also cleaned up the code.