Add Javadoc comments and clean up code