Added JavaDocs. Some small code optimizations.