Added Javadoc and improved code