Code documentation, minor cleanup and optimizations.