Cleans up code, fixes javadocs, applies tiny optimizations.